Safe to access the objectID property of an NSManagedObject from a different thread?
Safe to access the objectID property of an NSManagedObject from a different thread?
- Subject: Safe to access the objectID property of an NSManagedObject from a different thread?
- From: Ryan Meisters <email@hidden>
- Date: Sat, 26 Mar 2016 11:48:48 -0700
Hi!
I've inherited a codebase which uses MagicalRecord extensively. It seems that the recommended pattern for saving with MagicalRecord is something like this:
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *backgroundContext){
Person *backgroundPerson = [mainThreadPerson MR_inContext:backgroundContext];
backgroundPerson.firstName = @"John”;
backgroundPerson.lastName = @"Appleseed";
}];
The MagicalRecord documentation states that the save block will always run on a background thread. So `mainThreadPerson`, which is captured from the surrounding scope is used from the backgrond thread to access the objectID.
Is this ok? I believe the answer is no. The Core Data Programming Guide says:
"NSManagedObject instances are not intended to be passed between queues. Doing so can result in corruption of the data and termination of the application. When it is necessary to hand off a managed object reference from one queue to another, it must be done through NSManagedObjectID instances."
The author or this github issue https://github.com/magicalpanda/MagicalRecord/issues/448 raises the same concern. The basically says it's ok becuase "we're not accessing any Core Data properties at this point, we're accessing a ObjectID".
Is this really ok?
Kind Regards,
Ryan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden