Core Data: Deleting causes 'NSObjectInaccessibleException' from NSOperation with a reference to a deleted object

Posted by Bryan Irace on Stack Overflow See other posts from Stack Overflow or by Bryan Irace
Published on 2012-05-20T19:59:15Z Indexed on 2012/10/21 23:01 UTC
Read the original article Hit count: 276

My application has NSOperation subclasses that fetch and operate on managed objects. My application also periodically purges rows from the database, which can result in the following race condition:

  • An background operation fetches a bunch of objects (from a thread-specific context). It will iterate over these objects and do something with their properties.
  • A bunch of rows are deleted in the main managed object context.
  • The background operation accesses a property on an object that was deleted from the main context. This results in an 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault'

Ideally, the objects that are fetched by the NSOperation can be operated on even if one is deleted in the main context. The best way I can think to achieve this is either to:

  • Call [request setReturnsObjectsAsFaults:NO] to ensure that Core Data won't try to fulfill a fault for an object that no longer exists in the main context. The problem here is I may need to access the object's relationships, which (to my understanding) will still be faulted.
  • Iterate through the managed objects up front and copy the properties I will need into separate non-managed objects. The problem here is that (I think) I will need to synchronize/lock this part, in case an object is deleted in the main context before I can finish copying.

Am I missing something obvious? It doesn't seem like what I'm trying to accomplish is too out of the ordinary. Thanks for your help.

© Stack Overflow or respective owner

Related posts about core-data

Related posts about concurrency