multiple NSManagedObjectContexts and single NSPersistentStoreCoordinator
multiple NSManagedObjectContexts and single NSPersistentStoreCoordinator
- Subject: multiple NSManagedObjectContexts and single NSPersistentStoreCoordinator
- From: "Justin R. Miller" <email@hidden>
- Date: Sun, 28 Jan 2007 16:45:56 -0500
I have researched this in the list archives and believe I'm taking
the proper approach, but have a clarification question.
I need to access my Core Data store in a background thread. Thus, the
thread has its own managed object context to avoid having to lock the
main UI thread's context.
I am sharing the same persistent store coordinator between these two
contexts without any locking. This seems kosher according to Apple's
documentation here:
http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/
Articles/cdMultiThreading.html#//apple_ref/doc/uid/TP40003385
You can use one persistent store coordinator per group of
cooperating threads (for example, for your application or for each
document).
However, a bit further down is what seems like a contradiction to me:
If you do share a managed object context or a persistent store
coordinator between threads, and you invoke any methods on it, you
need to ensure that they are invoked from a thread-safe scope.
Typically this means you must lock the context or coordinator using
tryLock or lock. If you do this, the framework will ensure that
what it does behind the scenes is also thread-safe. For example, if
you create one context per thread, but all pointing to the same
persistent store coordinator, Core Data takes care of accessing the
coordinator in a thread-safe way ...
Is this only referring to accessing the coordinator directly across
threads (i.e. not when accessing it via a context)? Or in general?
Am I doing things safely here?
--
Justin R. Miller
Code Sorcery Workshop
http://codesorcery.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden