Animating Row Deletion in UITableView with CoreData gives Assertion failure
I have a UITableView that shows a list of objects stored with CoreData. I can delete an object using the following code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Delete row");
[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![managedObjectContext save:&error]) {
/*do this gracefully one day */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self refreshTables]; //where refreshTables just reloads the data the table is using and calls [self.tableView reloadData];
}
}
But it has no animation or aesthetic.
When I try to animate by replacing
[self refreshTables];
with
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
I get the following error:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], >/SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:920 2010-10-30 16:46:35.717 MyApp[38226:207] * 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 (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
I've tried having the deleteRowsAtIndexPaths code in a variety of places in the commitEditingStyle code with no luck (for example before removing the object from the mOC) but I can't seem to get around this e开发者_JAVA技巧rror.
I know Apple's iPhoneCoreDataRecipes example handles the problem by setting up a delegate for the FetchedResultsController to handle editing / deleting rows, but at this stage in development, if possible, I just want a simple solution for animating those deleted objects.
How can I animate the removal of a row, before/after I remove the object from my managedObjectContext?
EDIT: I've tried having deleteRowsAtIndexPaths before and after removing the item from the mOC, with the same error.
When we use NSFetchedResultsController as DataSource of UITableView, we can't invokedeleteRowsAtIndexPaths: withRowAnimation:
in function tableView: commitEditingStyle: forRowAtIndexPath:
, which will throw exception as question mentioned.
One way to solve this issue is by invoking [self.tableView reloadData]
in controllerDidChangeContent:
from protocol NSFetchedResultsControllerDelegate
. It actually solves, however, there is not Delete Fade Animation anymore.
So, the alternative convenient way is by invoking [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]
in controller: didChangeObject: atIndexPath: forChangeType:
newIndexPath:
.
Sample code as below:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Delete NSManagedObject
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
[context deleteObject:object];
// Save
NSError *error;
if ([context save:&error] == NO) {
// Handle Error.
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (type == NSFetchedResultsChangeDelete) {
// Delete row from tableView.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
1) I set up a correct delegate.
2) I removed a call in viewWillLoad to [self.tableview reloadData]; which was (oddly) messing everything up (this post gave me a clue for what to look for and remove: Serious Application Error in Core Data with fetchedResultsContainer ).
Are you using a NSFetchedResultsController at all?
You get this error because the object is still in your tableview datasource.
Maybe at this stage in development
you are using the simple solution
and populate NSArrays with objects from a NSFetchRequest. Then it will be pointless to remove the object from the managed object context
are you using a cache for the NSFetchedResultsController? I just had another look in the documentation and found this:
A controller thus effectively has three modes of operation, determined by whether it has a delegate and whether the cache file name is set.
No tracking: the delegate is set to nil. The controller simply provides access to the data as it was when the fetch was executed.
Memory-only tracking: the delegate is non-nil and the file cache name is set to nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes.
Full persistent tracking: the delegate and the file cache name are non-nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes. The controller maintains a persistent cache of the results of its computation.
So the mode of the controller is "No tracking". Which means objects are not removed from the controller if they are removed from the managed object.
What means just reloads the data
in the code of refreshTables
? Try to do the same before removing rows.
Or add those 20 lines needed to get the delegate working.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Delete row");
// Delete the row first
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//Then delete the object.
[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![managedObjectContext save:&error]) {
/*do this gracefully one day */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
精彩评论