Primary key attribute RestKit

I load data from a json file, I save it. I do this twice ... I got two entries in the sqlite Core Data database. Even if I set primaryKeyAttribute to mapping.

mapping.primaryKeyAttribute = @"code"; [mapping mapAttributesFromArray :mappedFields]; [[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:entityName]; 

My json

{"MyEntity": [{"code": "axv2", "data": "content"}]};

Here's the callback:

 - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { NSLog(@"Entries loaded %d",[objects count]); lastResult = objects; for(MyEntity * myEntity in lastResult) { [self saveContext]; } } 

My essence is correctly displayed ... But Restkit allows you to save duplicate entries with the same primary key?

Strange, I realized that this primary key attribute will avoid this problem.

+7
source share
2 answers

Starting with the latest version of RESTKit (0.23.2), you can define the primary key as follows:

 [_mapping addAttributeMappingsFromDictionary:@{ @"id" : @"objectId", @"name" : @"name" }]; [_mapping setIdentificationAttributes:@[ @"objectId" ]]; 

Whereas objectId is the primary key for the main data object.

+1
source

No, this is not so, since Core Data stores its own keys. You can easily solve this problem by checking if your primary key exists and before you save the corresponding instance of the object.

+1
source