So I know NSManagedObjects are not thread safe, and the best way to get objects from a background thread is to use [context objectWithId:id]; , and so pass around object ids instead of the actual object.
Let’s say I have a global NSManagedObject in my AppDelegate (not the best design pattern, but just for example) NSManagedObject *myObject;
Is it safe to access this object’s objectId from a background thread? Like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
objectId = appDelegate.myObject.objectId;
//is this thread safe?
}
This is not safe, since there’s no guarantee that calling
objectIDonmyObjectis safe. You should access the objectID outside of your block (on the main thread, for example) and then use it within your block. Something like: