Temporary NSManagedObjectIDs
I’ve run into a problem where I insert a managed object into a managed object context, in a separate thread, and then return the object ID to the main thread and then retrieve the object from the main thread’s context using that ID.
It didn’t always work. It is because I saved the first managed object context after I took the object ID. And the object ID was temporary because the object hadn’t been persisted. And the temporary object ID became invalid after the context was saved.
It didn’t always happen because my code will either insert or update when doing the JSON deserialisation. So if the object already existed in my persistent store, it would retrieve a proper object ID.
So the moral of the story is that you should always save your managed object context before getting the newly inserted object’s ID.
Member discussion