views:

661

answers:

1

Hi there, Fairly new to iPhone dev. My app uses Core Data, and at one point I'm attempting to save some data during an NSFetchedResultsControllerDelegate method:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath {

I'm getting most of this right out of Apple's documentation on the subject. The delete action looks like so:

case NSFetchedResultsChangeDelete:
        [managedObjectContext deleteObject:anObject];
        NSError *error;
        if (![managedObjectContext save:&error]) {
            NSLog(@"WTF? %@", [error description]);
        }
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        break;

Now, when I do this, the object in question will be deleted after the inevitable crash. So something right is happening. The crash is interesting, though:

Serious application error. Exception was caught during Core Data change processing:Failed to process pending changes before save. The context is still dirty after 100 attempts. Typically this recursive dirtying is caused by a bad validation method, -willSave, or notification handler. with userInfo (null)

2010-02-09 23:02:03.931 app2[26664:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to process pending changes before save. The context is still dirty after 100 attempts. Typically this recursive dirtying is caused by a bad validation method, -willSave, or notification handler.'

My research has turned up some very limited results -- good thing I have here to document this, eh? :-) But my gut is saying that the MOC is being written to elsewhere in my controller, though I can find no evidence of that happening. The viewDidLoad method has the NSFetchedResultsController being used to get the initial data, but that's it.

Any suggestions on what might be wrong here? As usual, your community-based hive mind is very much appreciated!

Cheers, Aaron

+3  A: 

(If I understand the problem correctly) I think all you need to do in this case is:

case NSFetchedResultsChangeDelete:

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        break;

Remember that method

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 

is notified after the underlying data has changed. So I believe your object is already deleted, you don't need to delete it manually. All that is left to be done in this method is remove it from the tableView.

Hope this helps!

peacefulfire
This is correct, the `-controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:` is fired as a result of a `NSManagedObjectContext` `-save:`. Therefore, when you call `-save:` inside of that method you are creating an endless loop.
Marcus S. Zarra
Thanks to both of you. Marcus, your note about the delegate method being called as a result of MOC save really crystallized it for me. I should go buy your book or something... :-)
Aaron Vegh