Last active
September 2, 2016 13:51
-
-
Save mikeabdullah/faa6fd7a75c04e7f9f9c to your computer and use it in GitHub Desktop.
Safer merge from saved context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mergeChangesFromSaveNotification:(NSNotification *)notification | |
intoContext:(NSManagedObjectContext *)context { | |
// NSManagedObjectContext's merge routine ignores updated objects which aren't | |
// currently faulted in. To force it to notify interested clients that such | |
// objects have been refreshed (e.g. NSFetchedResultsController) we need to | |
// force them to be faulted in ahead of the merge | |
NSSet *updated = [notification.userInfo objectForKey:NSUpdatedObjectsKey]; | |
for (NSManagedObject *anObject in updated) { | |
// The objects can't be a fault. -existingObjectWithID:error: is a | |
// nice easy way to achieve that in a single swoop. | |
[context existingObjectWithID:anObject.objectID error:NULL]; | |
} | |
[context mergeChangesFromContextDidSaveNotification:notification]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// above code will fault with multithread debugging enabled, so collect Ids, then do a block.
// http://mikeabdullah.net/merging-saved-changes-betwe.html