Question

Using RestKit 0.20.3. Placing post/put/delete (create/update/delete) calls in the didChangeObject:(id) … forChangeType method of the fetched results controller is not working out.

On a fetch of most recent entries from the database, the fetched results controller (FRC) gets an insert notification for a newly inserted retrieved record. That causes a POST that duplicates the object. The post itself causes two POSTS and five PUTS. The first POST is RestKit (RK) retrieving and ID. The second is the POST that caused the POST. The PUTS result from the updates from the two posts, each of which generates a change notification in the FRC. The main consequence of this is duplication of records that escalates in powers of two for each refresh.

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { switch (type) { case NSFetchedResultsChangeInsert: [self.timeEntryTableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; // call to RK enqueue a POST with the object via RKObjectManager break; case NSFetchedResultsChangeDelete: [self.timeEntryTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];//// // call to RK enqueue a DELETE with the object via RKObjectManager break; case NSFetchedResultsChangeUpdate: [self.timeEntryTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // call to RK enqueue a PUT with the object via RKObjectManager break; … } }

The question is, what is the right way? I believe the answer involves the core data relationships and saving. Am working on it. Not certain where this will lead. Pointers welcome.

A current answer to RestKit - Send added/edited/deleted objects after offline storage would help me.

Was it helpful?

Solution

I'm assuming that the server assigns the unique identifier for new objects, so when you create an object locally it doesn't have one to begin with.

The FRC is designed for updating table views. So, when you do a fetch you will get a number of inserts for objects that weren't in the FRC data set before but are now. Your code is misinterpreting this as new objects when really it isn't (most of the time).

You should check if the unique identifier is set and only POST a new object when it isn't. Technically you shouldn't get duplicates anyway if you are sending the unique id to the server as it could then identify the dupe.

For updates, you should consider adding a changed flag to your entity that is set whenever any attributes that should be uploaded are modified and reset after the upload is successful. Use this to filter PUTs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top