Adrian
Adrian

Reputation: 16715

Deleting an NSManagedObject from a managedObjectContext

I have a UITableViewController set up that displays my managedObjects properly when it's loaded, however when I go to delete a cell my app crashes. This is what the console has to say:

    2015-05-03 09:55:20.125 MyApp[9461:663817] *** Terminating app due to 
uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update:
 invalid number of rows in section 0.  The number of rows contained in an existing
 section after the update (1) must be equal to the number of rows contained in 
that section before the update (1), plus or minus the number of rows inserted or 
deleted from that section (0 inserted, 1 deleted) and plus or minus the number of
 rows moved into or out of that section (0 moved in, 0 moved out).'

When I reboot the app and go to my TVC, the object that I deleted the last time is deleted.

I have sections set up in my NSFetchedResultsController that is instantiated when viewDidLoad is called. I suspect I have a small issue, but I'm not sure where to go to add the missing line(s?).

I enable swipe to delete in viewDidLoad with this line:

// Enable swipe to delete
self.tableView.allowsMultipleSelectionDuringEditing = NO;

Here's my commitEditingStyle:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete selected NSManagedObject from managedObjectContext
        NSManagedObject *objectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        [self.managedObjectContext deleteObject:objectToDelete];
        [self.managedObjectContext save:nil];


        // Delete the row from the data source
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

Thank you for reading. If you have any ideas, I welcome them.

Upvotes: 0

Views: 187

Answers (1)

andykkt
andykkt

Reputation: 1706

The error message is saying that number of row returns from numberOfRowInSection is not matching with current number of item in table.. when you use fetchedResultsController numberOfRowsInSection should look something like this.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.fetchedResultsController fetchedObjects] count];
}

and fetchedResultsController delegate didChangeObject: is then called automatically which calls the deleteRowAtIndexPaths: inside.. so you don't need to call the deleteRowAtIndexPaths: just call deleteObject, otherwise it deletes twice.

Upvotes: 1

Related Questions