title: Core Data and multithreading
body: <p>I have read Marcus Zarra's chapter on multithreading in his Core Data book and have looked fairly closely at his sample code. But his code and others that I have found elsewhere seem to be focused in background processes that do <em>not</em> need to be aware of each other. These examples are good for importing a tree structure - but they do not address the importing of a more general (complex) structure, like a directed acyclic graph.</p>
  
  <p>In my case, I am trying to parse a C++ class hierarchy and would like to use as many NSOperations as possible. I would like to create an NSManagedObject instance for each encountered class and I would like to merge different NSManagedObjectContexts whenever one is saved.</p>
  
  <p>As an aside: I am able to get things working with a single NSOperation that iterates of files and parse one at a time. In this implementation, the -mergeChanges: approach that calls -mergeChangesFromContextDidSaveNotification: on main thread's MOC works well.</p>
  
  <p>But ideally, I would have one NSOperation iterate over source files and spawn NSOperations to parse each file. I have tried several approaches - but can't seem to get it right. The most promising was to have each NSOperation observing NSManagedObjectContextDidSaveNotification. With -mergeChanges: looking like this:</p>
  
  <pre><code>- (void) mergeChanges:(NSNotification *)notification
   {
   // If locally originated, then trigger main thread to merge.
   if ([notification object] == [self managedObjectContext])
    {
    AppDelegate *appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
  
    // Merge changes into the main context on the main thread
    [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
           withObject:notification
           waitUntilDone:YES];
    return;
    }
     // If not locally originated, then flag need to merge with in this NSOperation's thread.  
   [self setNeedsToMerge:YES];
   [self setMergeNotification:notification];
   }
  </code></pre>
  
  <p>Essentially, the parsing NSOperation's main() checked ivar 'needsToMerge' periodically. If it was true, then -mergeChangesFromContextDidSaveNotification: was called on local MOC with cached NSNotifications. And then needsToMerge was reset. If notification had originated locally, then main thread was told to perform -mergeChangesFromContextDidSaveNotification: on its MOC.</p>
  
  <p>I am sure that there is a good reason why this didn't work and why I get this:</p>
  
  <blockquote>
    <p>warning: Cancelling call - objc code
    on the current thread's stack makes
    this unsafe.</p>
  </blockquote>
  
  <p>I have also tried to use NSPeristentStoreCoordinator's lock to control access - but this is problematic if it is held during a call to NSManagedObjectContext's -save: method because -save: will notify interested observers of save event and -mergeChangesFromContextDidSaveNotification: appears to block trying to acquire PSC's lock.</p>
  
  <p>It just seems that this should be a lot easier.</p>