From 52dadca375ae77aad8449c7db6dcd87d215eb01b Mon Sep 17 00:00:00 2001 From: Nate Rivard Date: Thu, 30 Jul 2015 16:28:10 -0700 Subject: [PATCH] Only listen to private context changes. Fixes https://github.com/cruffenach/CRLoom/issues/7 --- CRLoom/CRLoom.m | 62 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/CRLoom/CRLoom.m b/CRLoom/CRLoom.m index 5ce6292..d1445c3 100644 --- a/CRLoom/CRLoom.m +++ b/CRLoom/CRLoom.m @@ -21,34 +21,6 @@ @interface CRLoom () @implementation CRLoom -- (void)setMoc:(NSManagedObjectContext *)moc { - _moc = moc; - __block CRLoom *blockSelf = self; - [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* note) { - - id object = note.object; - - if ([object isKindOfClass:[NSManagedObjectContext class]]) { - - NSManagedObjectContext *context = (NSManagedObjectContext*)object; - - if ([context.userInfo[kCRLoomContextKey] boolValue] && context != [CRLoom mainThreadContext]) { - CRLogCoreDataNote(note); - - //Fix for bug where NSFetchedResultsControllerDelegate's controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: - //isn't called when updates come in from a Managed Object Context on another thread. - //http://stackoverflow.com/questions/14018068/nsfetchedresultscontroller-doesnt-call-controllerdidchangecontent-after-update - - for(NSManagedObject *object in [[note userInfo] objectForKey:NSUpdatedObjectsKey]) { - [[blockSelf.moc objectWithID:[object objectID]] willAccessValueForKey:nil]; - } - - [blockSelf.moc mergeChangesFromContextDidSaveNotification:note]; - } - } - }]; -} - + (CRLoom*)loom { static dispatch_once_t once; static CRLoom *loom; @@ -69,10 +41,36 @@ + (NSManagedObjectContext*)mainThreadContext { } + (NSManagedObjectContext*)privateContext { - NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; - context.userInfo[kCRLoomContextKey] = @(1); - context.persistentStoreCoordinator = [self loom].moc.persistentStoreCoordinator; - return context; + NSManagedObjectContext *privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; + + __block CRLoom *blockSelf = [self loom]; + [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification object:privateContext queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* note) { + + id object = note.object; + + if ([object isKindOfClass:[NSManagedObjectContext class]]) { + + NSManagedObjectContext *context = (NSManagedObjectContext*)object; + + if ([context.userInfo[kCRLoomContextKey] boolValue] && context != [CRLoom mainThreadContext]) { + CRLogCoreDataNote(note); + + //Fix for bug where NSFetchedResultsControllerDelegate's controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: + //isn't called when updates come in from a Managed Object Context on another thread. + //http://stackoverflow.com/questions/14018068/nsfetchedresultscontroller-doesnt-call-controllerdidchangecontent-after-update + + for(NSManagedObject *object in [[note userInfo] objectForKey:NSUpdatedObjectsKey]) { + [[blockSelf.moc objectWithID:[object objectID]] willAccessValueForKey:nil]; + } + + [blockSelf.moc mergeChangesFromContextDidSaveNotification:note]; + } + } + }]; + + privateContext.userInfo[kCRLoomContextKey] = @(1); + privateContext.persistentStoreCoordinator = [self loom].moc.persistentStoreCoordinator; + return privateContext; } - (void)dealloc {