Search code examples
objective-cmacoscocoacore-datansarraycontroller

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.


Solution

  • 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];