Dev
Dev

Reputation: 7046

NSArrayController and the exception "CoreData could not fulfill a fault"

I have a list of items, instances of a Item class saved in a Core Data model.

This items are showed in a NSTableView using an NSArrayController and Cocoa Bindings. It works very well.

However, when I remove some items using these instructions:

// Removes selected items
for (Item *item in self.itemsArrayController.selectedObjects) {
    [self.managedObjectContext deleteObject:item];
}

NSError *error = nil;       
if (![self.managedObjectContext save:&error]) {
    [[NSApplication sharedApplication] presentError:error];
}

after some times, I obtain the exception CoreData could not fulfill a fault.

I read all the documentation that I found (including the Troubleshooting Core Data), but I did not find anything useful.

I'm using the new ARC (Automatic Reference Counting), so I'm pretty sure I'm not trying to access, after the save on the managed object context, the managed object which was deleted.

UPDATE: My app is single thread, so I'm not trying to access the managedObjectContext from multiple threads.

Upvotes: 0

Views: 529

Answers (2)

Aderstedt
Aderstedt

Reputation: 6518

You are enumerating the selected items of the array controller, and deleting the objects while enumerating. Try:

NSArray *selectedObjects = [[self.itemsArrayController selectedObjects] copy];
for (Item *item in selectedObjects) {
    [self.managedObjectContext deleteObject:item];
}
[selectedObjects release];

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

Are you accessing the same managedObjectContext on multiple threads? This feels like a race condition where you delete an object that the MOC expects to be around. A given NSManagedObjectContext can only be accessed from a single thread.

Upvotes: 1

Related Questions