title: Concurrency with core data
body: <p>I am using multi-threading to get data, parse it, create objects and store them. And after this is all done, I want the window to be shown.
  But now I have 2 issues:</p>
  
  <ul>
  <li>I have a deadlock </li>
  <li>My barrier does not act as a barrier.</li>
  </ul>
  
  <p>I think the deadlock is because I am updating the managedObjectContext in several threads at once. </p>
  
  <ol>
  <li><p>So I changed my managedObjectContext with the ConcurrencyType:</p>
  
  <pre><code>__managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  </code></pre></li>
  <li><p>And created an importContext for the concurrency queue and assigned the parentContext:</p>
  
  <pre><code>NSManagedObjectContext *importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
  importContext.parentContext = self.managedObjectContext;
  </code></pre></li>
  <li><p>And put my operations in a performBlock for the importContext:</p>
  
  <pre><code>[importContext performBlock:^{
  
    dispatch_async(backgroundQueue, ^{
    [myObject methodAWithContext:importContext];
    });
  
    dispatch_async(backgroundQueue, ^{
    [myObject methodBWithContext:importContext];
    });
  
    dispatch_async(backgroundQueue, ^{
    [myObject methodCWithContext:importContext];
    });
  
  
    dispatch_barrier_async(backgroundQueueM, ^{
    // create barrier to wait for the first 3 threads to be completed.
     dispatch_async(dispatch_get_main_queue(), ^{
  
      // Save the data from the importContext tot the main context on the main queue
      NSError *importError = nil;
      [importContext save:&amp;importError];
      [importContext.parentContext performBlock:^{
         NSError *parentError = nil;
         [importContext.parentContext save:&amp;parentError];
      }];
  
     [self.window makeKeyAndVisible];
  
     });
    });
  }];
  </code></pre></li>
  </ol>
  
  <p>Approach 1:
  In each method, I select a subset of object, delete these and then create new objects and save this.
  (I thought the delete was quicker than doing a fetch and check for the existence for every object to be created).
  So:
  In Method A I select all AObjects, delete them and create new AObjects.
  In Method B I select all BObjects, delete them and create new BObjects.
  In Method C I select all CObjects, delete them and create new CObjects.</p>
  
  <p>But then I get an error "An NSManagedObjectContext cannot delete objects in other contexts".</p>
  
  <p>So approach 2:
  I removed the delete. But now I get various different errors..... 
  And the barrier does not wait for the other threads to be executed.</p>
  
  <p>Q1: What am I doing wrong?</p>
  
  <p>Q2: how do I get the barrier to wait for the 3 threads to be completed</p>
  
  <p>Q3: how can I delete / purge objects on various threads?</p>
  
  <p>(I have read the Apple release notes and doc's, but I can't find this a clear explanation on the combination for multithreading and managedContext.)</p>