The Apple Concurrency with Core Data documentation is the place to start. Read it really carefully… I was bitten many times by my misunderstandings!
Basic rules are:
- Use one
NSPersistentStoreCoordinator
per program. You don’t need them per thread. - Create one
NSManagedObjectContext
per thread. - Never pass an
NSManagedObject
on a thread to the other thread. - Instead, get the object IDs via
-objectID
and pass it to the other thread.
More rules:
- Make sure you save the object into the store before getting the object ID. Until saved, they’re temporary, and you can’t access them from another thread.
- And beware of the merge policies if you make changes to the managed objects from more than one thread.
NSManagedObjectContext
‘s-mergeChangesFromContextDidSaveNotification:
is helpful.
But let me repeat, please read the document carefully! It’s really worth it!