RestKit / CoreData not updating - duplicate

I have an ios 5 application that does not create any data - it just makes a GET call to the REST web service and populates the sqlite database with these records. The original GET works fine when there are no records in the local database. However, when I make subsequent calls, I will only return a subset of records whose data has changed since the last GET. But what happens is that the records are simply added again, rather than updating existing records.

I have an identifier field, which is the primary key (or should be), and when a record arrives whose identifier already exists, I want this data to be updated. If this identifier does not exist, it must be an insert.

I have not seen a way to set my ID field as a "primary key" in the datamodel in Xcode. I tried to do this in my didFinishLaunchingWIthOptions method:

userMapping.primaryKeyAttribute = @"id";

But this itself really did nothing.

This is my call to execute a GET:

 // Load the object model via RestKit   
[objectManager loadObjectsAtResourcePath:[@"/synchContacts" appendQueryParams:params] delegate:self];

Which seems to do everything automatically. I lost at this point about where I should put the logic in order to check if the identifier exists, and if so, is the insert or something updated.

+3
source share
3 answers

As in the latest version of RESTKit (0.23), 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

, , , didLoadObjects , Core Data , .

, , . RestKit RKObjectMappingProvider

- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern withFetchRequestBlock:(RKObjectMappingProviderFetchRequestBlock)fetchRequestBlock;

fetchRequestBlock .

RestKit . , RestKit, .

0

: ID " " datamodel XCode. didFinishLaunchingWIthOptions: userMapping.primaryKeyAttribute = @ "id";

Keep in mind that "primaryKeyAttribute" is one of your api payloads, not a CoreData identifier, which CoreData manages independently. Then, RestKIt maps the (invisible) CoreData primary key to the specified JSON key.

0
source

All Articles