NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault

匆匆过客 提交于 2019-12-04 03:44:59

You could try and use existingObjectWithID:error::

Returns the object for the specified ID.

   - (NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error

Discussion

If there is a managed object with the given ID already registered in the context, that object is returned directly; otherwise the corresponding object is faulted into the context.

This method might perform I/O if the data is uncached.

Unlike objectWithID:, this method never returns a fault.

You could dO:

if ([myMOC existingObjectWithID:myObject.objectID error:&error])
    ...

You should verify that the object exists before accessing it's variables if you're having issues where the object may be deleted on another thread.

Two methods:

  1. Refresh the view datasources whenever your data is being deleted. You can do this by registering for the NSManagedObjectContextObjectsDidChangeNotification notification and then parsing the userInfo on that notification to see which object was deleted.
  2. Use code similar to below when you're passing data around to multiple threads.

Example:

// Cache and pass the object's ID off to another thread to do work on
// You can just store it as a property on the class
@try {
    NSManagedObject *theObject = [managedObjectContext objectWithID:self.theObjectID];

    // do stuff with object
}
@catch (NSException * e) {
    // An entity with that object ID could not be found (maybe they were deleted)
    NSLog(@"Error finding object: %@: %@", [e name], [e reason]);
}

You can check the NSManagedContext is existed when you use the NSManagedObject. like this:

if (obj.managedObjectContext)
{
    //do things
}

You can check [myObject isFault] where myObject is a NSManagedObject instance

You could give a try to use :

shouldDeleteInaccessibleFaults

property on managed object context. As this article says it should change the behaviour of faulting already deleted object.

https://cocoacasts.com/what-are-core-data-query-generations/

Edit: Since iOS 9 (when it was added) this property default value is YES.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!