title: Core data parent child synchronization
body: <p>I am having problems with the synchronization between child and parent managed object contexts.</p>
  
  <p>I have two contexts:</p>
  
  <ol>
  <li><code>mainContext</code> with <code>NSMainQueueConcurrencyType</code> as currency type.</li>
  <li><code>childContext</code> with mainContext as parent and the following configuration: <code>NSPrivateQueueConcurrencyType</code> and <code>NSOverwriteMergePolicyType</code></li>
  </ol>
  
  <p>I have also a model that we can call it as <code>Model</code>.
  I change some properties of all instances of <code>Model</code> in <code>childContext</code> when I need it:</p>
  
  <pre><code>[childContext performBlock:^{
      NSArray *allModels = [self retrieveAllModelsIn:childContext];
      for(Model *model in allModels) {
          [model updateSomeProperties];
      }
  
      if ([childContext hasChanges]) {
          NSError *error;
          [childContext save:&amp;error];
      }
  }];
  </code></pre>
  
  <p>I do the following in <code>mainContext</code>:</p>
  
  <pre><code>[mainContext performBlock:^{
      NSArray *allModels = [self retrieveAllModelsIn:mainContext];
      for(Model *model in allModels) {
          // Do some stuff with Model
          ...
  
          // Eventually I need to delete a model
          if (condition) {
              [mainContext deleteObject:model];
          }
      }       
  }];
  </code></pre>
  
  <p>Once everything is explained the problem is the following: Sometimes I get an object faultfill when I execute <code>[model updateSomeProperties]</code> in <code>childContext</code> because while <code>childContext</code> is iterating all models (retrieved previously), the <code>mainContext</code> may have deleted the object so the model is not valid when I am trying to do something on it on the <code>childContext</code>.</p>
  
  <p>My question is: What is the best way or the best practice to avoid this problem?</p>
  
  <p>Thanks!</p>