title: Core Data merge two Managed Object Context
body: <p>My Cocoa/Application has a Managed Object Context on the main thread.
  When I need to update my data my program will: </p>
  
  <ol>
  <li>Start a new thread</li>
  <li>Receive new data from a server</li>
  <li>Create a new Managed Object Context</li>
  <li>Send a notification to the main thread in order to merge the two context</li>
  </ol>
  
  <p>This is the function that receive the notification on the main thread</p>
  
  <pre><code>- (void)loadManagedObjectFromNotification:(NSNotification *)saveNotification
  {
      if ([NSThread isMainThread]) {
          [self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
      } else {
          [self performSelectorOnMainThread:@selector(loadManagedObjectFromNotification:) withObject:saveNotification waitUntilDone:YES];     
      }
  }
  </code></pre>
  
  <p>I do not receive any error.
  My problem is the merge result, it actually concatenate Managed Objects from both context.</p>
  
  <p>My Entity are a really simple list of attribute and relationship.</p>
  
  <p>Maybe the merge need some instructions in order to understand when an updated Managed Object IS NOT a new one, but a edited version of the first one. 
  I imagine that somewhere I need to specify a way to univocally identify an Entity, (an attribute for example can act like an ID) and something like a merge policy (if 2 managed object represent the same object, take the one with the lastModificationDate more recent).</p>
  
  <p>I just need to understand how to correctly merge the 2 contexts in order to have a single updated copy for each object.</p>
  
  <h1>UPDATE 1</h1>
  
  <p>The problem is now clear to me. 
  The 2 context has a big difference: the ObjectID. 
  While the context on the main thread fetched the ManagedObjects with the Persistent Store coordinator, the second thread create those object by fetching a remote URL. Even if the objects have the same contents, they will have 2 different objectID.</p>
  
  <p>My objects had already an unique identificator, I could use setObjectId in order to set this value. (Apple documentation says this is NOT a good idea).</p>