I'm having an issue related to objectIDs used in a multi-threaded context.
I am using objectIDs to pass an entity a completion block that's called by a background thread, like so :
NSManagedObjectID *entityID = [entity objectID];
[self.queue enqueue:
^{
NSManagedObjectContext *managedObjectContext = // child moc on a bg thread
Entity *entity = (Entity *)[managedObjectContext objectWithID:entityID];
/* Work using the entity */
}];
This used to work fine until I started deleting entities while network requests are ongoing ( Unfortunately, i cannot simply cancel them), which made me run into CoreData unresolved fault errors.
In order to fix this issue, I started using NSManagedObjectContext's existingObjectWithID: method instead. The advantage is that it will return nil if the object doesn't exist, but I ran into a different issue : existingObjectWithID will return nil if the objectID is temporary, although the object truly exists.
I tried using obtainPermanentIDsForObjects when entities are created, but this isn't ideal since it implies some work with the persistence store, and randomly crashes.
Is there a proper way to check that an entity still exists upon the completion of an asynchronous network request ?
Thanks in advance !