Skip to content

Instantly share code, notes, and snippets.

@scottmkroberts
Created September 20, 2013 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmkroberts/6635878 to your computer and use it in GitHub Desktop.
Save scottmkroberts/6635878 to your computer and use it in GitHub Desktop.
Fix issue with managedObjectContextDidSave notification.
/* 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