title: Correct implementation of parent/child NSManagedObjectContext
body: <p>My app sometimes inserts objects into the managed object context that are not meant to necessarily be saved. For example, when I launch an 'add entity' modal, I create a managed object and assign it to the modal. If the user saves from that modal, I save the context. If he cancels, I delete the object and no save is necessary.</p>
  
  <p>I have now introduced an 'import' feature that switches to my app (using a URL scheme) and adds an entity. Because one of these modals might be open, it is not safe to save the context at this point. The transient object created for the modal will be saved, even if the user cancels, and there is no guarantee that the deletion (from the cancel operation) will be saved later - the user might quit the app.</p>
  
  <p>Similarly, I can't simply save whenever my app quits. If the modal is open at that point, the temporary object will be incorrectly saved.</p>
  
  <p>To address this I am attempting to use a child context, as discussed <a href="http://www.cocoanetics.com/2012/07/multi-context-coredata/" rel="noreferrer">here</a>. Having read all I could find on SO, I sill have a few questions:</p>
  
  <ol>
  <li><p>Which concurrency type should I be using for each context? Remember that I am not doing this for performance/threading benefits. I know I can not use NSConfinementConcurrencyType for the main context if it is to have child contexts, but I'm not sure which of the other two options is best suited. For the child context, does it need to match? Or can I even use the confinement type here? I've tried a variety of combinations and all seem to work ok, but I would like to know which is appropriate for my requirements.</p></li>
  <li><p>(side issue) Why can I only get this to work if I use a class iVar? I thought I should be able to declare the temporary context in the method where it is created, and then later refer to it using entity.managedObjectContext. But it seems to be nil by the time I come to access it? This is rectified if I instead use an iVar to hold the reference.</p></li>
  <li><p>What is the correct way or propagating the change to the main context? I have seen various comments using different block-wrapped implementations on each of the contexts. Does it depend on my concurrency type? My current version is:</p>
  
  <pre><code>//save the new entity in the temporary context
  NSError *error = nil;
  if (![myObject.managedObjectContext save:&amp;error]) {NSLog(@"Error - unable to save new object in its (temporary) context");}
  
  //propogate the save to the main context
  [self.mainContext performBlock:^{
      NSError *error2 = nil;
      if (![self.mainContext save:&amp;error2]) {NSLog(@"Error - unable to merge new entity into main context");}
  }];
  </code></pre></li>
  <li><p>When my user saves, it sends its delegate (my main view controller) a message. The delegate is passed the object that was added, and it must locate that same object in the main context. But when I look for it in the main context, it is not found. The main context <strong>does</strong> contain the entity - I can log its details and confirm it is there - but the address is different? If this is meant to happen (why?), how can I locate the added object in the main context after the save?</p></li>
  </ol>
  
  <p>Thanks for any insight. Sorry for a long, multi-part question, but I thought somebody was likely to have addressed all of these issues previously.</p>