views:

1118

answers:

2

I have a UITableViewController managing a grouped tableView. The tableView is populated from a fetchedResultsController.

If I click the Edit button in the NavigationBar, then select a row and click the Delete button, the row is deleted and all ends well.

However, if I swipe to reveal the Delete button in a row and click the Delete button, the app crashes with the following error:

2010-01-06 15:25:18.720 Take10[14415:20b] Serious application error. Exception was caught during Core Data change processing: -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)

2010-01-06 15:25:18.721 Take10[14415:20b] Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

Of course, the index number in the error changes depending on the number of rows in the section where I am attempting to delete a row, and that number is 1 more than the number of remaining rows in the table section after the attempted delete.

Here is the code where I attempt to delete the data from the fetchedResultsController. The same method responds to both scenarios, so I don't understand why it crashes when swiping.

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

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the managed object for the given index path
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
  }   
}

Any ideas???

Thanks

Jk

A: 

One difference between normal and swipe delete is that the latter will call tableView:willBeginEditingRowAtIndexPath and tableView:didEndEditingRowAtIndexPath. In fact, that's a good way of suppressing the indentation and showing of an insert row.

Another difference is that setEditing: is called (with NO as the parameter value) immediately after the delete.

Set breakpoints in any of those three functions that you've defined/overridden and see if you can narrow down where it's happening.

Frank Schmitt
A: 

Jeff LaMarche has done some good work with NSFetchedResultsController.

Try using his template here: http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html

And see if that solve your issue.

Corey Floyd
Bought his book "More iPhone 3 Development. It's been a big help.Jk
Alpinista