I am passing some NSManagedObject data between two threads using an NSOperationQueue with a concurrency level of up to max 1, and I would like to get some suggestions about whether I am doing this correctly.
Since NSManagedObject is not thread safe, I am sending NSManagedObjectID from ThreadA (main thread) to ThreadB through the derived NSOperation class. Total Workflow:
ThreadA (main thread):
- creates an NSPersistentStoreCoordinator
- creates the main NSManagedObjectContext (1) file
creates an NSManagedObjectContext (2) for use in workThread
creates MyNSOperationItem, iterates over NSManagedObjectContext and adds MyNSOperationItem to NSOperationQueue
ThreadB (thread NSOperationQueue):
- The derived NSOperation class will retrieve data from a persistent store using the supplied object ID.
My NSOperation class is as follows:
@interface MyNSOperationItem: NSOperation
{
NSManagedObjectContext *threadedMOC;
NSManagedObjectID *workItemObjectID;
}
@end
So, is reference to NSManagedObjectContext normal for my derived NSOperation class, or should I store the second NSManagedObjectContext in another place? Since this is the queue, multiple instances of MyNSOperationItem will be created, each of which points to the same NSManagedObjectContext.
source
share