Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use Core Data in a multithread fashion, but you should follow one of the <a href="http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/Articles/cdConcurrency.html#//apple_ref/doc/uid/TP40003385-SW1" rel="nofollow">approaches recommended by Apple</a>:</p> <blockquote> <p>The pattern recommended for concurrent programming with Core Data is thread confinement: each thread must have its own entirely private managed object context.</p> <p>There are two possible ways to adopt the pattern:</p> <p>Create a separate managed object context for each thread and share a single persistent store coordinator. This is the typically-recommended approach.</p> <p>Create a separate managed object context and persistent store coordinator for each thread. This approach provides for greater concurrency at the expense of greater complexity (particularly if you need to communicate changes between different contexts) and increased memory usage.</p> </blockquote> <p>Particularly:</p> <blockquote> <p>Using thread confinement, you should not pass managed objects or managed object contexts between threads. To “pass” a managed object from one context another across thread boundaries, you either:</p> <p>Pass its object ID (objectID) and use objectWithID: or existingObjectWithID:error: on the receiving managed object context. The corresponding managed objects must have been saved—you cannot pass the ID of a newly-inserted managed object to another context.</p> <p>Execute a fetch on the receiving context. These create a local version of the managed object in the receiving context.</p> </blockquote> <p>From this, it ensues that you cannot create managed objects in a thread (with its own context) then save them in the other.</p> <p>So, in order to accomplish what you would like to, you need to share a managed object context or a persistent store coordinator between threads. In this case you should properly use locking techniques to prevent your store from entering an inconsistent state:</p> <blockquote> <p>If you share a managed object context or a persistent store coordinator between threads, you must ensure that any method invocations are made from a thread-safe scope. For locking, you should use the NSLocking methods on managed object context and persistent store coordinator instead of implementing your own mutexes. These methods help provide contextual information to the framework about the application's intent—that is, in addition to providing a mutex, they help scope clusters of operations.</p> </blockquote> <p>I would not suggest to go this route, if you have any chance to modify the design of your app so that you do not need going into locking and synchronization for your core data object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload