CoreData with multithread

Posted by Daniel Vela on August 9, 2012

If we use CoreData in a multithreaded applications, we must take count of the following. A NSManagedObject created in one thread, can’t be used in another thread. Also a NSManagedObjectContext can’t be used inter threads.

Two practices are recommended when programming with threads and CoreData:

  1. Create a new NSManagedObjectContext for each thread. The object NSPersistentStore can be shared among thread safely.
  2. When is necessary sharing a NSManagedObject between threads, share its objectID property only. Then query that object with method:
		[managedObjectContext objectWithID:objectID];

As apple say:

apple doc

PS: Remember that when using asynchronous request -either using blocks or delegates-, always is used a different thread for the response treatment.