Created
          March 30, 2012 17:28 
        
      - 
      
 - 
        
Save bjhomer/2253154 to your computer and use it in GitHub Desktop.  
    Deadlocking with a parent context
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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