Skip to content

Commit

Permalink
Only listen to private context changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Rivard committed Jul 30, 2015
1 parent 8534ab0 commit 52dadca
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions CRLoom/CRLoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 52dadca

Please sign in to comment.