Created
September 20, 2013 10:52
-
-
Save scottmkroberts/6635878 to your computer and use it in GitHub Desktop.
Fix issue with managedObjectContextDidSave notification.
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
/* Can be called from any thread */ | |
- (void)managedObjectContextDidSave:(NSNotification *)note | |
{ | |
// FIX: you must mergeChangesFromContextDidSaveNotification: on main thread | |
// handling the notification occurs on background thread. | |
// it needs to "merge" on the main thread. | |
// From: http://benford.me/blog/2012/5/7/an-observer-of-nsmanagedobjectcontextdidsavenotification-html | |
NSManagedObjectContext *context = (NSManagedObjectContext *)note.object; | |
if( context.persistentStoreCoordinator == managedObjectContext.persistentStoreCoordinator ){ | |
[managedObjectContext performSelectorOnMainThread:@selector(managedObjectContextDidSave:) | |
withObject:managedObjectContext waitUntilDone:NO]; | |
} | |
/* | |
UMTRACE; | |
@try | |
{ | |
if ([NSThread isMainThread]) | |
{ | |
[managedObjectContext mergeChangesFromContextDidSaveNotification:note]; | |
} | |
else | |
{ | |
[self performSelectorOnMainThread:@selector(managedObjectContextDidSave:) | |
withObject:note | |
waitUntilDone:NO]; | |
} | |
} | |
@catch (NSException *exception) | |
{ | |
DebugLog(@"Exception caught in when merging change insert changes with fetch:\n %@", exception); | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment