I am trying to adopt WWDC 2012 sample code for iCloud and CoreData ( link to github sample code ) and need some help to understand what is going on there.
I have a table view that takes its contents from NSFetchedResultsController, as in the example. The NSFetchedResultsController is associated with the main NSManagedObjectContext of the CoreDataController shown in the example. Changes in inputs are synchronized from one device to another, and it works like a charm. But there is no place where changes from iCloud are actually merged into the main context. I have seen many examples where NSPersistentStoreDidImportUbiquitousContentChangesNotification is used to merge changes, but never happens in this code.
But here's the weird thing: I take an object from the main context and hold it. If I get NSPersistentStoreDidImportUbiquitousContentChangesNotification, I use objectId and reload the object from NSMangedObjectContext:
NSManagedObjectID* objectId = [myObject objectID]; NSManagedObject* theNewObject = [[_coreDataController mainThreadContext] objectWithID:objectId]; myObject = theNewObject;
But the object is not updated. Even if I select with a predicate using the unique property of the object. But table view with NSFetchedResultsController shows the changes. What am I missing here?
EDIT 1 (After reading Tom's answer):
I added an observer for NSPersistentStoreDidImportUbiquitousContentChangesNotification :
- (void)iCloudupdate:(NSNotification*)note { NSManagedObjectContext* moc = [[CoreDataController sharedController] mainThreadContext]; [moc performBlock:^{ [moc mergeChangesFromContextDidSaveNotification:note]; [self refreshObject]; }]; }
The refreshObject method uses NSManagedObjectID to retrieve the object from the main MOC, but it is still an old version.
source share