title: Core Data and multithreading (and Bindings to make it more fun)
body: <p>I have got this background thread that does a few things with core data objects. I get the context as follows:</p>
  
  <pre><code>- (id)_managedObjectContextForThread;
  {    
      NSManagedObjectContext * newContext = [[[NSThread currentThread] threadDictionary] valueForKey:@"managedObjectContext"];
      if(newContext) return newContext;
  
      newContext = [[NSManagedObjectContext alloc] init];
      [newContext setPersistentStoreCoordinator:[[[NSApplication sharedApplication] delegate] persistentStoreCoordinator]];
      [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(_mergeChangesFromManagedObjectContext:)
                                               name:NSManagedObjectContextDidSaveNotification
                                             object:newContext];
  
      [[[NSThread currentThread] threadDictionary] setValue:newContext forKey:@"managedObjectContext"];
      return newContext;
  }
  </code></pre>
  
  <p>then I fetch some objects, modify them and save the context:</p>
  
  <pre><code>- (void) saveContext:(NSManagedObjectContext*)context {
      NSError *error = nil;
      if (![context save:&amp;error]) {
          [[NSApplication sharedApplication] presentError:error];
      }
  }
  
  - (void)_mergeChangesFromManagedObjectContext:(NSNotification*)notification;
  {
      [[[[NSApplication sharedApplication] delegate] managedObjectContext] performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                                                                                      withObject:notification
                                                                                   waitUntilDone:YES]; 
  }
  </code></pre>
  
  <p>.. later I remove the observer. This works for the main part. But some properties don't get updated when they get merged back. The properties that were nil before get updated. The ones that had a value stay the same.</p>
  
  <p>I tried:</p>
  
  <pre><code>[newContext setMergePolicy:NSOverwriteMergePolicy];
  </code></pre>
  
  <p>... (and the other merge policies) on the main context but it did not work :P</p>
  
  <p>Thank you for your help.</p>
  
  <p>Note: I have bound the values to a NSTableView. I log them after the merge. The values properties that were nil seem to work fine.</p>