Create a Grand Central Dispatch queue that will be in only one thread for Core Data

Is there a way to create a GCD queue that runs on only one thread? I want to use this queue to handle master data operations.

+3
source share
2 answers

Yes; just create a queue with a flag DISPATCH_QUEUE_SERIAL(this is also the default). But be careful when moving kernel data operations to another thread. This NSManagedObjectContextshould only be used in one thread, so you will need two contexts: one for your main thread and one for your background thread. Performing this correctly may require some caution.

, UIManagedDocument ( iOS5), . , , .

+4

, concurrency. NSPrivateQueueConcurrencyType NSMainQueueConcurrencyType, MOC . ...

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
// Do other MOC initializations stuff...
[moc.performBlock:^{
    // All code in this block will run in the proper thread for the Managed Object Context.
}];

FWIW, NSMainQueueConcurrencyType , . executeBlock MOC, NSConfinementConcurrencyType, . .

0

All Articles