Search code examples
ioscore-datansnotifications

What am I supposed to do in processContentChanges:?


This is my app's processContentChanges: method, which is triggered by NSPersistentStoreDidImportUbiquitousContentChangesNotification:

- (void)processContentChanges:(NSNotification *)notification {    
    [self.managedObjectContext performBlock:^{
        // Merge incoming data updates in the managed object context
        [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];

        // Post notification to trigger UI updates
#warning What do I actually do here?
    }];
}

I'm using NSFetchedResultsControllers throughout my app so that the UI updates automatically when changes are received from another device through iCloud. This all seems to work, but the comment saying // Post notification to trigger UI updates was there in the template method already. Am I actually supposed to do something here, or can I safely leave things the way they are?


Solution

  • Well, although I haven't had confirmation of this I don't think there is anything else that needs to be done in this method as long as the following criteria are met:

    1. You implement NSPersistentStoreDidImportUbiquitousContentChangesNotification correctly as per the template so that new content is merged to the managed object context
    2. Your content is generated using NSFetchedResultsController objects
    3. Your viewControllers conforms to the the NSFetchedResultsControllerDelegate protocol, and implements controllerWillChangeContent:, controllerDidChangeContent: and controller:didChangeObject:atIndexPath:forChangeType:newIndexPath
    4. In those methods, update your views accordingly to display new content, remove deleted content, and update changed content.

    If you have any objects which utilise CoreData without an NSFetchedResultsController then you might need to update these by manually re-fetching the data when NSPersistentStoreDidImportUbiquitousContentChangesNotification is posted by CoreData.