Duplicates appear in the database after fetching.
Below are the mapping and the fetch code:
+(RKEntityMapping *)userMapping:(RKEntityMapping *)userMapping
{
[userMapping addAttributeMappingsFromDictionary:@{
@"userid" : @"userid",
@"firstName" : @"firstName",
@"lastName" : @"lastName",
}];
userMapping.identificationAttributes = @[@"userid"];
return userMapping;
}
Fetch Code:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.objectManager = [self getObjectManager];
self.objectManager.managedObjectStore = appDelegate.managedObjectStore;
self.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:self.objectManager.managedObjectStore];
userMapping = [RESTMappingProvider userMapping:userMapping];
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"findUser" statusCodes:statusCodeSet];
NSMutableURLRequest *request = [self.objectManager.HTTPClient requestWithMethod:@"GET"
path:@"/findUser"
parameters:@{@"userid": userSearch
}];
//request.cachePolicy = NSURLRequestReloadIgnoringCacheData;
[self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.savesToPersistentStore = NO;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSError *error;
for (User *foundUser in mappingResult.array)
{
foundUser.lastUpdated = [NSDate date];
}
if ([self.managedObjectContext saveToPersistentStore:&error])
{
NSLog (@"******* User OBJECTS SAVED **********");
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
});
}];
[operation start];
[operation waitUntilFinished];
}
}
LOG:
2014-07-27 14:25:48.756 App[36854:5b3b] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2014-07-27 14:25:48.756 App[36854:3803] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'User': {
email = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = email;
};
firstName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = firstName;
};
lastName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = lastName;
};
lastUpdated = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = lastUpdated;
};
userid = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = userid;
};
}
2014-07-27 14:25:48.757 App[36854:5b3b] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
firstName = Lois;
lastName = Lane;
userid = "lois.lane";
} with mapping <RKEntityMapping:0x12d78e80 objectClass=User propertyMappings=(
"<RKAttributeMapping: 0x12d819d0 userid => userid>",
"<RKAttributeMapping: 0x12d85f70 lastName => lastName>",
"<RKAttributeMapping: 0x12d72a10 firstName => firstName>",
)>
2014-07-27 14:25:49.476 App[36854:3803] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2014-07-27 14:25:49.477 App[36854:3803] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
firstName = Lois;
lastName = Lane;
userid = "lois.lane";
} with mapping <RKEntityMapping:0xd6c17a0 objectClass=User propertyMappings=(
"<RKAttributeMapping: 0xd6b6ed0 userid => userid>",
"<RKAttributeMapping: 0xd6c86b0 lastName => lastName>",
"<RKAttributeMapping: 0xd6c1560 firstName => firstName>",
)>]
Database:
How can I avoid duplicates?
EDIT 1
- (RKManagedObjectStore *)setupCoreDataWithRESTKit
{
NSError * error;
NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]];
NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[self.managedObjectStore createPersistentStoreCoordinator];
NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentPath = [searchPaths objectAtIndex:0];
NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/App.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
if(!persistentStore){
NSLog(@"Failed to add persistent store: %@", error);
}
[self.managedObjectStore createManagedObjectContexts];
return self.managedObjectStore;
}