Delete last line in & # 8594; crash using NSFetchedResultsController

I am using NSFetchedResultsController. I used to have a similar problem, when there are no records in the database to represent the table, but then one is created, I ended up in at least one section there, so I fixed it. But now it crashes when I have, for example, two partitions, each with one row, and I delete one row, so the section should be deleted -> crash. It states that the number of partitions before updating (2) is not equal to the number of 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 A method in which a row is 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); } } } 
+4
source share
2 answers

I found a problem / solution:

I did not have the required didChangeSection delegation 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; } } 
+10
source

I had the same problem. However, simply implementing the controller: didChangeSection: atIndex: forChangeType method did not fix my failure. I needed to update several table views (deleting a partition + moving a row from a newly deleted partition to an existing or new one) would happen in one atomic update. I just added:

 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 
0
source

Source: https://habr.com/ru/post/1400106/


All Articles