Core data backed UITableView

  • Hi all,
    Here is the dumb question for the day.

    I have a coredata backed UITableView.  I populate a NSFetchedResultsController and use that to source the rows and sections etc.  All works well.
    That is until I go to delete an item from the table.

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"commitEditingStyle called");
    TheEntity *myEntity = (VideoEntity *) [fetchedResultsController objectAtIndexPath:indexPath];

    NSManagedObjectContext *context = [[self fetchedResultsController] managedObjectContext];
    [context deleteObject:myEntity];

    //commit the delete
    NSError *saveError = nil;
    if (![context save:&saveError]) {
      NSLog(@"CoreData save Failure");
    }else {
    //all good removed from coredata
    //now delete from tableView

    [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    Then I get an error like

    *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-984.38/UITableView.m:774
    *** 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).'

    This appears to mean I need to remove it from the fetchedResultsController before I remove it from the table.  So as NSFetchedResultsController has no remove facility I reload it.  Is there a more efficient way of achieving this?

    Regards
    Damien
  • On Mar 15, 2010, at 5:33 pm, Damien Cooke wrote:

    > [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    >
    Why are you doing this rather than (in addition to? which if so would probably be the cause of the problem) simply implementing the fetched results controller delegate methods as shown in the documentation (<http://developer.apple.com/iphone/library/documentation/CoreData/Reference/
    NSFetchedResultsControllerDelegate_Protocol/Reference/Reference.html
    >) and in the Navigation-based Application template?

    mmalc
previous month march 2010 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today