Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: 'NSObjectInaccessibleException' when attempting to delete.

Tags:

ios

core-data

Core Data is allowing me to save, but when I try to delete the object, I'm getting the following error:

*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xa6b7e00 <x-coredata://8A687ECB-03F8-47C0-8470-087B9CF032B1-2801-00000A8C9A09CDB7/Notification/p2774005D-4B49-4378-A109-949F15D37032>''

[self.managedObjectContext executeFetchRequest:allFetchRequest onSuccess:^(NSArray *results) {
                NSLog(@"Fetch for Objects with origial_id");
                if (results.count>0)
                {
                    NSLog(@"original_id =  %@", dlPlanDate.original_id);
                    NSLog(@"Results Recieved %i", results.count);
                    for (PlanDate *pd in results)
                    {
                        PlanDateResolved *pdResolved = [NSEntityDescription insertNewObjectForEntityForName:@"PlanDateResolved" inManagedObjectContext:self.managedObjectContext];
                        NSArray *keys = [[[pd entity] attributesByName] allKeys];
                        NSDictionary *dict= [pd dictionaryWithValuesForKeys:keys];
                        [pdResolved setValuesForKeysWithDictionary:dict];
                        [pdResolved setValue:[pd plandate_id] forKey:[pdResolved primaryKeyField]];
                        pdResolved.senderUser = pd.senderUser;
                        pdResolved.objectCreator = pd.objectCreator;
                        pdResolved.users = pd.users;
                        NSLog(@"pd.planDateID %@", pd.plandate_id);
                        [self.managedObjectContext saveOnSuccess:^{

                        NSLog(@"Saving Each Object in the Array  to PlanDateResolved with Original ID");
                            [self.managedObjectContext deleteObject:pd];
                            [self.managedObjectContext saveOnSuccess:^{
                                NSLog(@"Deleted the pd Object object!");
                            } onFailure:^(NSError *error) {
                                NSLog(@"There was an error Deleting pd Object %@", error);
                            }];
                        } onFailure:^(NSError *error) {
                        }];
                    }
                }
            } onFailure:^(NSError *error) {
                NSLog(@"Error fetching: %@", error);
            }];
        }

Log:

2013-10-11 11:53:11.175 ST[2801:c07] Fetch for Objects with origial_id
2013-10-11 11:53:11.175 ST[2801:c07] original_id =  CFADD5A7-C9E8-48B3-91B2-56FDBC0F9BAB
2013-10-11 11:53:11.175 ST[2801:c07] Results Recieved 3
2013-10-11 11:53:11.180 ST[2801:c07] pd.planDateID CFADD5A7-C9E8-48B3-91B2-56FDBC0F9BAB
2013-10-11 11:53:11.182 ST[2801:c07] pd.planDateID 5917071A-8CFD-4C7C-A60F-A76FA4814429
2013-10-11 11:53:11.183 ST[2801:c07] pd.planDateID 1998BB90-D6FC-4226-977C-3414C4A57078
2013-10-11 11:53:11.913 ST[2801:c07] You updated the Accepted object!
2013-10-11 11:53:11.913 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:11.913 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:12.155 ST[2801:c07] Saving Each Object in the Array  to PlanDateResolved with Original ID
2013-10-11 11:53:35.482 ST[2801:c07] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xa6b7e00 <x-coredata://8A687ECB-03F8-47C0-8470-087B9CF032B1-2801-00000A8C9A09CDB7/Notification/p2774005D-4B49-4378-A109-949F15D37032>''
*** First throw call stack:
(0x24e7012 0x1cfae7e 0x1a20a48 0x1a515e8 0x1ac2879 0x1a59627 0x1a59271 0x1ace025 0x1a53cc1 0x1e7f014 0x1e6ed5f 0x1e6eaa3 0x1a53c8b 0x1a53aee 0x1a30eac 0x1ab0c1 0x1a5398f 0x1e7f014 0x1e6f7d5 0x248daf5 0x248cf44 0x248ce1b 0x24027e3 0x2402668 0x92effc 0x418d 0x25c5)
libc++abi.dylib: terminate called throwing an exception
like image 481
user1107173 Avatar asked Oct 11 '13 22:10

user1107173


4 Answers

When a managed object is inaccessible it means that Core Data was not able to find that object in the persistent store file. There are a couple of reasons this can happen.

I don't know where executeFetchRequest:onSuccess: and saveOnSuccess: come from, but assuming they do what their method names imply (and are not horribly buggy), there's no obvious reason in the code above why this would happen. So it's happening as a side-effect of work you're doing somewhere else.

Most likely: You're also deleting these objects somewhere else, and this delete happens sometime after you perform the fetch but before the code crashes. As a result they're already gone when the code above tries to delete them. It appears that the mystery Core Data methods you're using run asynchronously (based on the fact that you get pd.planDateID printed multiple times before reaching the crash point), so this could be either on the same thread or on a different thread.

[All of this assumes that your log statements actually came from the code you posted. That's somewhat in doubt, because the logs include a message reading "You updated the Accepted object!", which does not appear in the code.]

like image 134
Tom Harrington Avatar answered Oct 23 '22 20:10

Tom Harrington


I agree with Tom Harrington about the root issue explanation. Additionally it is also good to know that since iOS 9 this behavior has changed. On iOS 9 there is no NSObjectInaccessibleException thrown but a CoreData warning will occur like:

CoreData: warning: An NSManagedObjectContext delegate overrode fault
handling behavior to silently delete the object with ID '0xd0000000025c0006
<x-coredata://336D9F02-727B-42C5-AEDD-8007D4AB521D/Film/p151>' and substitute 
nil/0 for all property values instead of throwing.
2015-09-10 15:27:09.799 PROJECTX[2141:818058] CoreData: error: Mutating a
managed object 0xd0000000025c0006 <x-coredata://336D9F02-727B-42C5-AEDD-
8007D4AB521D/Bla/p151> (0x12812bea0) after it has been removed from its 
context.

This will not cause a crash but an ambiguous app behavior.

like image 10
dzensik Avatar answered Oct 23 '22 18:10

dzensik


This will generally happen in case you try to access the NSManagedObject in a wrong NSManagedObjectContext. If you are using managed object from UI thread (to display data directly, or manipulation on main thread), you need to use NSMainQueueConcurrencyType concurrency.

Also if you are using object on background thread, it should be chile context with NSPrivateQueueConcurrencyType concurrency.

like image 3
Vinay Chavan Avatar answered Oct 23 '22 20:10

Vinay Chavan


Look at the time stamp of your error. Clearly, you have tried to access the deleted managed object somewhere else, quite a bit later. Tom's and Rey's explanation is correct. The object is inaccessible because it is not present in the persistent store.

You should check in your code where you still might have references to the managed object and make sure you are not trying to access it once it has been deleted.

like image 2
Mundi Avatar answered Oct 23 '22 18:10

Mundi