Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving a temporary managed object context on a background queue
    primarykey
    data
    text
    <p>According to the <a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html" rel="nofollow">Concurrency with Core Data Guide</a>, you should not save a NSManagedObjectContext in a background thread, because it is possible for the app to quit before the save completes, since threads are detached.</p> <p>If I'm understanding correctly, that means something like this is incorrect</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSManagedObjectContext* tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [tempContext setParentContext:[[MyDataManager sharedInstance] mainContext]; [tempContext performBlockAndWait:^{ //Do some processing NSError* error; [tempContext save:&amp;error]; }]; }); </code></pre> <p>My first instinct would be to just save the context on the main queue when it's finished, but managedObjectContexts are supposed to be thread safe. Is the following something that would solve the problem or is there a better solution?</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSManagedObjectContext* tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [tempContext setParentContext:[[MyDataManager sharedInstance] mainContext]; [tempContext performBlockAndWait:^{ //Do some processing }]; dispatch_async(dispatch_get_main_queue(), ^{ [tempContext performBlockAndWait:^{ NSError* error; [tempContext save:&amp;error]; }]; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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