title: Synchronous Distributed Objects Over NSConnection Issue
body: <p>I have an application that pulls data from the web, parses them, and compiles the results in a search interface.  Since the data are not co-dependant, it made sense to multi-thread the application to execute multiple fetches and parses simultaneously.  I use NSInvocationOperation on a search and parse object I have written to execute this function.</p>
  
  <p>In the controller object I have the following method:  </p>
  
  <pre><code>-(void) searchAndParseAsynchronously { 
   NSPort *serverPort = [NSMachPort port];
   NSConnection *serverConnection = [NSConnection connectionWithReceivePort:serverPort sendPort:serverPort];
   [serverConnection setRootObject:self];
   for (NSURL *urlToProcess in self.urlsToFetch)
   {
    BaseSearchParser *searcherForURL = [BaseSearchParser newSearchParserWithParameters:self.searchParams];
    searcherForURL.urlToDocument = urlToDocument;
  
    SearchThreader *searchThreader = [SearchThreader new];
    searchThreader.threadConnection = comConnection;
    searchThreader.targetSchema = searcherForURL; 
    NSInvocationOperation *threaderOperation = [[NSInvocationOperation alloc] initWithTarget:searchThreader 
                                                                                          selector:@selector(executeSearchParse) 
                                                                                            object:nil];
    [self.operationQueue addOperation:threaderOperation];
   }
  }
  </code></pre>
  
  <p>The application relies on Core Data, which I've gathered is mostly thread unsafe.  I have a different NSManagedObjectContext for each search/parse operation (and one for the controller), and only pass the NSManagedObjectId between operations or proxy objects.</p>
  
  <p>The operations communicate the completed parse results back to their controller via an NSConnection object.  The controller constructs the NSConnection using an NSMachPort object, sets itself as the root object, and gives the same NSConnection object to each of the targets of the NSInvocationOperations.  The controller then enqueues the NSInvocationOperation for execution in its own NSOperationQueue.</p>
  
  <p>In the search threader object I have the following method:</p>
  
  <pre><code>-(void) executeSearchAndParse
  { 
   id parentServer = [threadConnection rootProxy];
   [parentServer setProtocolForProxy:@protocol(SearchParseProtocol)];
  
   NSArray *importResults = [targetSchema generatedDataSetIds];
  
   [parentServer schemaFinished:targetSchema];
   [parentServer addSearchResults:importResults];
  }
  </code></pre>
  
  <p>I believe I have followed the Apple example of generic inter-thread communication given <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DistrObjects/DistrObjects.html" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I have to say that most of the time, this works beautifully: The notifications from the NSConnection rootProxy are posted to the run loop in the main thread as expected and wait for pickup until the controller object is ready.  However, in some of my test cases it causes Core Data to grind to a screeching halt because sometimes the messages make it to the controller object <em>in the same thread as the NSInvocationOperation object which is calling the rootProxy object</em>.  </p>
  
  <p>I've placed a debugger point in the controller on the message which is sent when the search/parse operation is complete, and sure enough, sometimes (just sometimes) the executing thread is <em>not</em> the main one.  Does anyone have an idea as to why this might occur?  Or, is there a simpler way to construct inter-thread asynchronous communication?  OR is my approach for Core Data totally off-kilter?</p>
  
  <p>Thanks in advance!</p>