title: Core Data concurrency - NSManagedObjectContext
body: <p>So I need to use cora-data managed object context using multiple thread.
  I found the below implementation to be pretty much what I need.
  I have 2 question :</p>
  
  <ol>
  <li><p>Where do I release the MOC that I allocate ? I don't want to go through every function thread in my app and to call a function to release the MOC.
  I'd rather write the function, and somehow make it so it be called by every thread prior to his exit. is that possible ?</p></li>
  <li><p>Is the below implementation enough for core data concurrency ?</p>
  
  <pre><code>-(void) StoreManagedObjectContextForCurrentThread:(NSManagedObjectContext*) context
  {
      [[[NSThread currentThread] threadDictionary] setObject: context forKey: AQPerThreadManagedObjectContext];
  }
  
  
  -(NSManagedObjectContext *) myManagedObjectContext {
  
  
      NSManagedObjectContext * result = [[[NSThread currentThread] threadDictionary] objectForKey: AQPerThreadManagedObjectContext];
      if ( result != nil )
          return result;
  
      NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
      if (coordinator!=nil)
      {
          NSManagedObjectContext * moc = [[NSManagedObjectContext alloc] init];
          [moc setMergePolicy: NSMergeByPropertyObjectTrumpMergePolicy];
          [moc setPersistentStoreCoordinator: coordinator];
          [self StoreManagedObjectContextForCurrentThread:moc];
          [moc release];      // now owned by the thread dictionary
          return moc;
      }
  
      return nil;
  }
  </code></pre></li>
  </ol>