Alua K Alua K - 1 year ago 87
iOS Question

Deleting a row in tableview without deleting Core Data Entity

I have a table view backed by NSFetchedResultsController.
The situation is as follows.
I have implemented "swipe to delete" a row. Delegate methods of NSFetchedResultsController and those of table view's are called. But the thing is, I don't want to delete a Core Data Entity, I just want to mark its boolean flag since I need this entity in other parts of application, and want table view to delete row with animation. However, according to error messages it is against logic like if you are updating an entity, update a row, not delete it.
Is there any elegant way to achieve it? Or the only option is reload the table?

Implementation of swipe to delete

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
Issue *issue = [self.fetchedResultsController objectAtIndexPath:indexPath];
[issue setHasBeenDeleted:[NSNumber numberWithBool:YES]];
[issue setPurchased:[NSNumber numberWithBool:NO]];
[self.managedObjectContext saveToPersistentStore:nil];

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id) anObject
atIndexPath:(NSIndexPath *)indexPath
newIndexPath:(NSIndexPath *)newIndexPath


case NSFetchedResultsChangeUpdate:

[self.tableView deleteRowsAtIndexPaths:@[indexPath]


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];

Error message

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 (5) must be equal to the number of rows contained in that section before the update (4), 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)

Answer Source

In the fetch request you are giving to your FRC, include this boolean value as a part of your predicate. For example, [NSPredicate predicateWithFormat:@"shouldDisplay = YES"]. Then whenever you want to remove are particular object, just set your shouldDisplay (or whatever you call it) to NO, and you will receive an FRC delegate callback for controller: didChangeObject: with a NSFetchedResultsChangeDelete change type. In this callback, you will do the following.

NSIndexPath *indexPath = [controller indexPathOfObject:anObject];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

Your object will remain in core data, but it will be filtered out of this particular table view because of your predicate on the boolean flag.

EDIT: You also need to implement two of the FRC delegate methods to notify your table view to begin and end updates. This will correct the error you are receiving. Like so:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

    [self.tableView beginUpdates];

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

    [self.tableView endUpdates];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download