Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>The way you are using <code>saveUsingCurrentThreadContextWithBlockAndWait:</code> is dangerous at least.</p> <p>Method <code>saveUsingCurrentThreadContextWithBlockAndWait:</code> seems to be designed for a "thread confinement" approach, where a managed object context is associated to a <em>particular</em> thread.</p> <p>However, you are invoking the method from within a block which is dispatched to a dispatch queue. There is no direct relation between a queue and a thread, that is there is no such particular thread. Thus, <code>saveUsingCurrentThreadContextWithBlockAndWait:</code> won't work here for you. You can expect crashes.</p></li> <li><p>The reason why you see a lot of work executed on the main thread, is that (probably) the <em>default</em> managed object context, where the persistent store coordinator has been assigned, is associated to the main thread. In other words, all the <em>heavy</em> work - namely saving and fetching the model objects to and from the persistent store is executed on the <em>main</em> thread. </p> <p>It sounds weird, that it is explicitly recommended (in the docs) to have the default context associated to the <em>main</em> thread - even it's doing the heavy work. It makes probably more sense to have a managed object context which is using a private queue which is the ultimate "root" context where the persistent sore is assigned to. Any child context may then be either associated to the main thread or any other private dispatch queue. </p> <p>It may be reasonable to have the default context execute on the main thread, but it should have a parent context, and the ultimate root context shall handle the persistent store and should execute on a private queue.</p></li> </ol> <p>Edit:</p> <p>After digging into the sources of MagicalRecord, I have to say, that method <code>saveUsingCurrentThreadContextWithBlockAndWait:</code> is actually wonky: it creates a context with a private queue, which is not suitable for thread confinement. Expect something bad happening.</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