Skip to content

Instantly share code, notes, and snippets.

@Januzellij
Created February 16, 2014 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Januzellij/9040581 to your computer and use it in GitHub Desktop.
Save Januzellij/9040581 to your computer and use it in GitHub Desktop.
I've got a UITableView backed by an array of Core Data objects, made by appending two arrays of Core Data objects. Whenever I try to delete a row from the table view, it throws:
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 (2) must be equal to the number of rows contained in that section before the update (2), 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).'
Here's the code to delete (in my UITableViewCell subclass):
NSManagedObjectContext *context = [NSManagedObjectContext MR_contextForCurrentThread];
[object MR_deleteInContext:context];
[context MR_saveToPersistentStoreAndWait];
[tableViewController loadData];
[tableView deleteRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationLeft];
Here's the loadData method (basically refetches the array):
NSPredicate *textParentFilter = [NSPredicate predicateWithFormat:@"parent == %@", self.parent];
self.notes = [Note MR_findAllWithPredicate:textParentFilter];
NSPredicate *folderParentFilter = [NSPredicate predicateWithFormat:@"parentFolder == %@", self.parent];
self.folders = [Folder MR_findAllWithPredicate:folderParentFilter];
My table view's numberOfRowsInSection is [self.notes count] + [self.folders count]
I can see why I'm getting an exception: I'm deleting the row in loadData and then the numberOfRowsInSection is out of sync. However, if I replace deleteRowsAtIndexPaths with [tableView reloadData], it deletes just fine (but there's no animation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment