Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Created March 30, 2012 17:28
Show Gist options
  • Save bjhomer/2253154 to your computer and use it in GitHub Desktop.
Save bjhomer/2253154 to your computer and use it in GitHub Desktop.
Deadlocking with a parent context
NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
NSManagedObjectContext *mainQueueContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
mainQueueContext.parentContext = backgroundContext;
[backgroundContext performBlock:^{
id someManagedObject = [backgroundContext objectWithID:someID];
// Ensure that the object is not a fault
someManagedObject.someProperty;
[mainQueueContext performBlockAndWait:^{
id mainThreadObject = [mainQueueContext objectWithID:someID];
mainThreadObject.someProperty;
// This will try to fulfill the fault. Since the parent context has the
// object in its local context, it will try to pull it straight from that
// context. BUT THAT CONTEXT IS BLOCKED in performBlockAndWait.
//
// DEADLOCK! To fix, use performBlock: instead of performBlockAndWait:
}];
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment