I want to use Core Data in background threads and NSOperationQueue. Now I thought why shouldn't I just create a class holding all the Core Data stack things with a property of NSManagedObjectContext, and then just make this property atomic?
One thing I'm not sure about @synchronize(self) {...}
is this: Assume I have a property that's considered thread-safe:
@property (retain) NSManagedObjectContext *moc;
Every NSOperation or thread accesses an CoreDataTools
class which has this moc property. They access this moc property like this:
// Assume: Inside an NSOperation or new thread here...
NSManagedObjectContext *moc = [[CoreDataTools sharedInstance] moc];
// Do a lot of things with moc. Add and remove 20 objects.
Would this be effective at all? I don't understand WHEN this lock is effective. Just in the event of actually accessing the property? Or can I safely work with the NSManagedObjectContext instance until the NSOperation or called method of the background thread is done and returns?
I know NSMangagedObjectContext has a -lock method. I want to know in general if this would work or not.