13

During swype deleting (most importatnt lines of this method):

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    Table *deleteRow = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.managedObjectContext deleteObject:deleteRow];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  }   
}

When deleting row I get this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2.
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).'

If I comment last line of code ([tableView deleteRowsAtIndexPaths:...]) everything works fine (but I have to refresh view to see that row was deleted).

How to do it properly..?

EDIT: Considering @Kyr Dunenkoff responce I've added:

- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
    UITableView *tableV = [self tableView];
    switch(type) {
        case NSFetchedResultsChangeDelete:
            [tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
    [[self tableView] endUpdates];
}

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
    [[self tableView] beginUpdates];
}

However this didn't change the crashing & error. Atm it just caused that adding new rows isn't working any more.

5 Answers 5

14

To do this properly, implement controller:didChangeObject: method, within it make a switch for different change types, within NSFetchedResultsChangeDelete case insert your [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];. Because when you work with data source like NSFetchedResultsController, all changes must come from there and your table only reflects them. Additionally, you can implement controller:willChangeContent and controller:didChangeContent, put [tableView beginUpdates] into willChange and [tableView endUpdates] into didChange.

2
  • 1
    Please check my upper update, fix isn't working as i implemented it (maybe wrong..?). Thanks for responce!
    – Nat
    Jan 23, 2012 at 16:47
  • Hm. What's in your [tableView:numberOfRowsInSection:] method? It should have something like return [[self.fetchedResultsController fetchedObjects] count];. Jan 23, 2012 at 17:27
8

The problem is - as you've discovered by commenting it out - your last line:

[tableView deleteRowsAtIndexPaths:...

The apple Table View Programming Guide tells you need to do that, but in fact you're not supposed to when using NSFetchedResultsController. The idea of NSFetchedResultsController is that it takes care of updating the UI to keep it in sync with the model so that you don't need to. Hence, if you simply delete the object from the model, the fetched results controller will take care of the rest.

[Update] Whoops, not entirely correct what I said just there. Fetched results controller will not just "take care of the rest" without help. It's delegate will take care of the rest, but someone needs to implement it's delegate. I'm in the habit of including the CoreDataTablewViewController helper class from the Standford CS193p course, which is simply a UITableViewController subclass that implements all of the methods recommended in the documentation of NSFetchedResultsController. Most importantly, it implements the delegate methods that update the table view when the model changes.

So your updated approach is still correct: you should NOT put the deleteRowsAtIndexPaths.. in the code that causes the user to delete them, but let the fetched results controller drive the process, and implement the delegate to actually do it.

(get the helper class here: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall, and watch lecture 14 to see how to use it)

2

Kyr's answer is correct, but here is a discrete example of how to delete an NSManagedObject from your database as well as from from the NSFetchedResultsController:

- (void) deleteObjectFromDBAndTable:(NSIndexPath *)indexPath forTable:(UITableView*)tableView
{
    NSLog(@"Deleting object at row %d", indexPath.row);

    // Delete the object from the data source
    NSManagedObject *objToDelete = (NSManagedObject*)[self.fetchedResultsController objectAtIndexPath:indexPath];

    // Delete the object from the database
    [DBHandler deleteObject:objToDelete andSave:YES];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch (type)
    {
        case NSFetchedResultsChangeDelete:

            [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

            break;
        default:
            break;
    }
}
2

You need to make sure you don't call [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; yourself. Deleting the object from the context will trigger the NSFetchedResultsController to refresh itself AND the table.

0

As told by Rhubarb i implemented these following delegates and problem solved

//Fetch delegates implementation from web.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall (CoreDataTableViewController.zip)
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        self.itemsTableView.beginUpdates()
        beganUpdates = true
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
        switch type {
            case .insert:
                self.itemsTableView.insertSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            case .delete:
                self.itemsTableView.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            default:
                break
        }
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

        switch type {

            case .insert:
                self.itemsTableView.insertRows(at: [newIndexPath!], with: .fade)
            case .delete:
                self.itemsTableView.deleteRows(at: [indexPath!], with: .fade)
            case .update:
                self.itemsTableView.reloadRows(at: [indexPath!], with: .fade)
            case .move:
                self.itemsTableView.deleteRows(at: [indexPath!], with: .fade)
                self.itemsTableView.insertRows(at: [newIndexPath!], with: .fade)
            default:
                break
        }
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    if beganUpdates {
        self.itemsTableView.endUpdates()
    }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.