title: Core Data - break retain cycle of the parent context
body: <p>Let's say we have two entities in a Core Data model: Departments and Employees.<br>
  The Department has a one-to-many relationship to Employees.</p>
  
  <p>I have the following ManagedObjectContexts:<br>
  - Root: connected to the Persistent Store Coordinator<br>
  - Main: context with parent Root</p>
  
  <p>When I want to create an Employee I do the following:<br>
  - I have a Department in the Main context<br>
  - I create an Employee in the Main context<br>
  - I assign the Department to the Employee's department property<br>
  - I save the Main context<br>
  - I save the Root context</p>
  
  <p><em>This creates a retain cycle both in the Main context and in the Root context.</em></p>
  
  <p>If I did this without a child context (all in the Root context), then I could break the retain cycle by calling <code>refreshObject:mergeChanges</code> on Employee. In my situation with the two contexts I could still use that method to break the cycle on the Main context, but how am I going to break the cycle on the Root context?</p>
  
  <p><em>Side note: this is a simple example to describe my problem. In Instruments I can clearly see the number of allocations growing. In my app I have contexts that go deeper than one level, causing an even greater problem, because I get a new entity allocation with retain cycle per context I'm saving.</em></p>
  
  <p><strong>Update 15/04: NSPrivateQueueConcurrencyType vs NSMainQueueConcurrencyType</strong><br>
  After saving both contexts I can perform <code>refreshObject:mergeChanges</code> on the Main context with the Department object. This will, as expected, re-fault the Department object, break the retain cycle and deallocate the Department and Employee entities in that context.</p>
  
  <p>The next step is to break the retain cycle that exists in the Root context (saving the Main context has propagated the entities to the Root context). I can do the same trick here and use <code>refreshObject:mergeChanges</code> on the Root context with the Department object.</p>
  
  <p>Weird thing is: this works fine when my Root context is created with NSMainQueueConcurrencyType (all allocations are re-faulted and dealloced), but doesn't work when my Root context is created with NSPrivateQueueConcurrencyType (all allocations are re-faulted, but <strong>not</strong> dealloced).</p>
  
  <p><em>Side note: all operations for the Root context are done in a performBlock(AndWait) call</em></p>
  
  <p><strong>Update 15/04: Part 2</strong><br>
  When I do another (useless, because there are no changes) save or rollback on the Root context with NSPrivateQueueConcurrencyType, the objects seem to be deallocated. I don't understand why this doesn't behave the same as NSMainQueueConcurrencyType.</p>
  
  <p><strong>Update 16/04: Demo project</strong><br>
  I've created a demo project: <a href="http://codegazer.com/code/CoreDataTest.zip" rel="noreferrer">http://codegazer.com/code/CoreDataTest.zip</a></p>
  
  <p><strong>Update 21/04: Getting there</strong><br>
  Thank you Jody Hagings for your help!<br>
  I'm trying to move the <code>refreshObject:mergeChanges</code> out of my ManagedObject <code>didSave</code> methods.  </p>
  
  <p>Could you explain to me the difference between:</p>
  
  <pre><code>[rootContext performBlock:^{
      [rootContext save:nil];
      for (NSManagedObject *mo in rootContext.registeredObjects)
          [rootContext refreshObject:mo mergeChanges:NO];
  }];
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[rootContext performBlock:^{
      [rootContext save:nil];
      [rootContext performBlock:^{
          for (NSManagedObject *mo in rootContext.registeredObjects)
              [rootContext refreshObject:mo mergeChanges:NO];
      }];
  }];
  </code></pre>
  
  <p>The top one doesn't deallocate the objects, the bottom one does.</p>