Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are asking for fits right in with nested contexts. You create a private-queue context, and attach it directly to the persistent-store-coordinator. It is simple. Take your current code, and, instead of this...</p> <pre><code>managedObjectContext = [[NSManagedObjectContext alloc] init]; </code></pre> <p>replace it with this...</p> <pre><code>workerManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; </code></pre> <p>Now, you have replaced your traditional confinement MOC with a new MOC that will run with its own concurrency queue.</p> <p>To get your context that you can use from the main thread, you create another managed object context, and make it a child of the one you just created...</p> <pre><code>managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; managedObjectContext.parent = workerManagedObjectContext; </code></pre> <p>This means that you can use managedObjectContext just like you were before. Now, however, instead of it going directly to the store, it goes through an intermediary context. The parent context will perform work in its own background thread.</p> <p>Thus, you can make all the changes to managedObjectContext that you want. When it comes time to save, you do something like this...</p> <pre><code>static void saveManagedObjectContext(NSManagedObjectContext *moc, void(^completionBlock)(NSError *error)) { [moc performBlock:^{ NSError *error = nil; if (moc.hasChanges &amp;&amp; [moc save:&amp;error] &amp;&amp; moc.parentContext) { saveManagedObjectContext(moc.parentContext, completionBlock); } else { completionBlock(error); } }]; } </code></pre> <p><strong>EDIT</strong></p> <p>If you want to use this universally, you can easily add it to a category on NSManagedObjectContext, and then just call...</p> <pre><code>[managedObjectContext saveWithCompletionBlock:^(NSError *error){ if (error) { // Handle the error return; } // Handle success... }]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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