Like a lot of iOS Devs I’m used to working with my domain objects across various different threads.
As you may know, when you decide to work with an object on a different thread, you need to retrieve that object from that thread’s NSManagedObjectContext. This is normally done like so:
The record’s objectId is a universal identifier and you can find out more information from the Apple Docs on it.
However, I was recently having trouble with saving an object in one thread and then retrieving it in another thread. It was throwing an exception when I tried to use the object on the second thread.
The problem is that the initial objectId on a record is a temporary one. Therefore, when we hop onto a different thread, we can’t do a lookup on it.
The solution? Simply make the following call on your original managedObjectContext like so
[recordInFirstContext.managedObjectContext obtainPermanentIDsForObjects:@[recordInFirstContext] error:&error];
This will force the record to create a permanent objectId which can then be retrieved on another thread.