Implementation of NSOperation and CoreData

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
{
    // MyNSOperationItem is created in thread1 and MOC will be 
    // set on creation
    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.

+5
source share
1 answer

I think this should give you everything you need:

http://developer.apple.com/mac/libra...reData/Articles/cdMultiThreading.html

, , :

, . (, , SQLite, XML- .) , , . , , . BackgroundFetching ///CoreData/.

. . ( , , ), ( objectWithID: ).

+5

All Articles