Search code examples
ioscore-dataicloud

Extract specific update from NSPersistentStoreCoordinator


I'm currently using iCloud and CoreData to sync data across my app, so every time notification fires, I update my local array of data. The problem I am running into is that my data set is getting large and I don't want to update the entire set of data every time there is a new notification.

Basically, I have an Entity called Photo, and every time the user makes an update to one Photo object on device A, it gets synced with iCloud, which then gets pushed to device B. The device receives the notification through:

persistentStoreDidImportUbiquitousContentChanges:

which looks like this:

notification.userInfo.description: { deleted = "{(\n)}"; inserted = "{(\n 0x17045c80 56E70CB19352/Photo/p8431>\n)}"; updated = "{(\n)}"; }

I'd like to grab that specific insertion, update, or deletion and apply it to my local array instead of iterating through the entire set of fetchedObjects.

I tried casting the insertion object to a Photo object, but that didn't work. Any thoughts on how to extract that info?

Thanks!


Solution

  • The objects in that notification are instances of NSManagedObjectID. You can use those with your NSManagedObjectContext to retrieve the managed objects. Use existingObjectWithID:error: (safe, potentially slow) or objectWithID: (fast, potentially less safe).

    You probably don't need to do that, though. You can take that notification and pass it to mergeChangesFromContextDidSaveNotification: to merge any changes it contains. If you need to do manual merging, you can, but it's usually not needed.