NSObjectInaccessibleException ', reason: "CoreData could not complete the error

My iOS application uses kernel data across multiple threads. I get several crash reports with the following message: "NSObjectInaccessibleException", reason: "CoreData could not execute the error for" 0x1e07a9b0 "

I understand what causes this problem is that the object was deleted, but another thread is trying to access it. I am working on a solution to the problem, but I want to add a check to the background thread to see if the object is to blame this way.

My code currently refers to myObject.myValue . Is it possible to perform some verification, for example:

 if (!myObject.myValue) { return; } 

... so that he leaves the method before doing anything that could cause such a crash? Or just call myObject.myValue , even if it is zero, throw such an exception?

+7
source share
4 answers

You can try and use existingObjectWithID:error: ::

Returns the object for the specified identifier.

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

Discussion

If there is a managed object with the given identifier already registered in the context, this object is returned directly; otherwise, the corresponding object will be damaged in context.

This method can perform I / O operations if the data is not locked.

Unlike objectWithID: this method never returns an error.

Could you:

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

You must make sure that the object exists before accessing it, if you have problems when the object can be deleted in another thread.

Two methods:

  • Update view data sources when deleting data. You can do this by registering for an NSManagedObjectContextObjectsDidChangeNotification notification and then userInfo in that notification to find out which object has been deleted.
  • Use code similar to the one below when you are transmitting data across multiple streams.

Example:

 // Cache and pass the object 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]); } 
+5
source

You can verify that NSManagedContext exists when you use NSManagedObject. eg:

 if (obj.managedObjectContext) { //do things } 
+2
source

You can check [myObject isFault] where myObject is an instance of NSManagedObject

0
source

All Articles