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

后端 未结 5 1782
野性不改
野性不改 2021-02-15 14:27

My iOS app uses core data via multiple threads. I am getting some crash reports with the following message: \"\'NSObjectInaccessibleException\', reason: \'CoreData could not ful

5条回答
  •  孤街浪徒
    2021-02-15 14:39

    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]);
    }
    

提交回复
热议问题