title: Confusion when using multiple NSManagedObjectContexts with Relationships
body: <p>I'm using four NSManagedObjectContexts to manage the insertion and deletion of data retrieved from a source stored on a server. The relationships between the MOC's have been set as follows.</p>
  
  <pre><code>_masterManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
   [_masterManagedObjectContext setPersistentStoreCoordinator:_psc];
  
  _mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:initWithConcurrencyType:NSMainQueueConcurrencyType];
   [_mainManagedObjectContext setParentContext:self.masterManagedObjectContext];
  
   _insertionContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
  [_insertionContext setParentContext:self.mainManagedObjectContext];
  
  _cacheContext = _insertionContext;
  </code></pre>
  
  <p>As you can see the masterMOC is linked to the persistantStoreCordinator and handles the persistence of the data. The mainMOC has a parent relationship with the masterMOC and is used to provide the FRC on the UI with data and help the FRC delegate to monitor changes made from refreshing the data. The insertionMOC has a parent relationship with the mainMOC and is created on a separate thread using an NSOperation and has been instantiated in the main method call. The cacheMOC is passed insertionMOC from the insertion class to inherit its properties. I have all this working as advertised above but… when it comes to refreshing the data my app crashes. </p>
  
  <p>The way I see it the issue is that there is some confusion between the contexts as to what the correct set of ManagedObjectIDs are after insertionMOC has deleted them.</p>
  
  <p>This is how I delete the old ManagedObjects</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:_insertionContext]];
  [fetchRequest setIncludesPropertyValues:NO];
  NSError *error;
  NSArray *array = [NSArray array];
  array = [_insertionContext executeFetchRequest:fetchRequest error:&amp;error];
  [self.insertionContext obtainPermanentIDsForObjects:array error:&amp;error];
  
  if (array.count &gt;0 &amp;&amp; [self.kEntity isEqualToString:@"Event"]) {
       [array enumerateObjectsUsingBlock:^(Event *obj, NSUInteger idx, BOOL *stop) {
       [_insertionContext deleteObject:obj];
  }];
  </code></pre>
  
  <p>Note. If I don't obtainPermenentObjectIDsForObjects: error: the app will crash at this point.</p>
  
  <p>After this has run I don't save the inertionContext becasue this will trigger a refresh of the UI with the FRC delegate monitoring changes. Instead I collect the new data and insert some of the results for one Entity into cache. For the cache to work it needs to create a fetch request to obtain the managedObjectIDs and find the duplicates. It's at this point the app crashes.</p>
  
  <p>My understanding of the documentation for MOCs with nested relationships is that a fetchRequest will pulled down the data though all the MOCs from those saved in the persistantStore. But if this was the case I shouldn't need to force insertionMOC to obtainPermanentOBJectIDs when fetching them to delete them. Having said this, it appears my cache is able to obtain the permanentIDs from the persistanStore and as a result it's crashing.</p>
  
  <p>Should the cacheMOC be set with a relationship to the insertionMOC as the other are? SHould the cache also be set to occur on it's own private thread thread to get this to work? Should I be locking the mainMOC to prevent the FRC updating the results and then saving the insertionMOC to persist the deletes?</p>