i use NSFetchedResultsController to populate tableview and every thing is ok(adding, deletng..).. But whenever i try to remove last row from tableview(when there is only one row in tablview) the app crashes with the log below:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037
2012-08-03 16:32:39.667 MackaWithCoreData[793:15503] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 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). with userInfo (null)
As far as i see, after deleting the object when the delegate methods try to reload the tableview since the fetchedResultsController is null it causes a crash.. How to handle this issue? Thanks in advance
EDIT Implementation of controller controllerDidChangeContent:... method is below
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
XLog(@"the row removed from tv");
break;
case NSFetchedResultsChangeUpdate:
// [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
XLog(@" object deleted from fetchedresultscontroller");
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
After a few days i decided a different logic to solve this issue. I firstly control the count of
fetchedObjectsonfetchedResultsController, if the count is greater than 1 i call deletion & saving delegates on database, but if the count is equal to 1, just push to a different viewController. On that vc too, i’ll disable navigation controller back to this view controller untill a new message comes. And when a new message comes, i will firstly delete the object on entity and than add the message..Here is some pseudo code & algorithm:
// on the class of target vc
I posted this stupidly coded pseudo to help others who faces the same situation, if anyone knows a better approach, please write down.
Below is my code: