Reputation:
When I don't synthesize
the NSFetchedResultsController
property
manually my app crashes.
Crash Log:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
2013-03-06 15:09:15.667 Staff Manager[6673:c07] *** 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 (0) 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, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
When I synthesize
it as follow all works as expected:
@synthesize fetchedResultsController = _fetchedResultsController;
Can you explain?
My state of knowledge is that Xcode's compiler does this for me.
Xcode Version: 4.6 Deployment Target: 6.1
App crashes in this method (last line)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
Person *personToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:personToDelete];
[self.managedObjectContext save:nil];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
[self.tableView endUpdates];
}
}
Upvotes: 2
Views: 688
Reputation: 1866
All I can say is when you use create a project from xcode with an already created nsfetchresultscontroller, the fetchresultscontrollerdelegate is created in a way it already perform the deleteRowsAtIndexPaths when you delete a displayed managedobject it automatically delete the corresponding tableview cell
you should only keep the deleteObject part
Person *personToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:personToDelete];
[self.managedObjectContext save:nil];
Upvotes: 1