Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting last row in a section -> crash, using NSFetchedResultsController

I'm using NSFetchedResultsController. Previously I had a similar issue when the database has no entries for the tableview but then one is created, I turned out there has to be at least one section, so I fixed that. But now it crashes when I have for example two sections, each with one row and I delete one row, so section should be gone -> crash. It says that the number of sections before the update (2) is not equal to the number deleted (0).

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return 1 if the fetchedResultsController section count is zero
    return [[fetchedResultsController sections] count] ? : 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // check if we really have any sections in the managed object:
    if (!fetchedResultsController.sections.count) return @"Persoonlijk";

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // check if we really have any sections in the managed object:
    if (!fetchedResultsController.sections.count) return 0;

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

Update Method where row gets deleted:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete schedule
        NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
        [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

        // Save the context.
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);
        }
   } 
}
like image 543
Pieter Avatar asked Dec 27 '22 04:12

Pieter


1 Answers

I found the problem/solution:

I did not have the required didChangeSection delegate method for this situation!

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSLog(@"didChangeSection");

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
like image 181
Pieter Avatar answered Apr 08 '23 07:04

Pieter