核心数据严重的应用程序错误在controllerDidChangeContent

你好,我现在堆积在下面的错误。

CoreData:错误:严重的应用程序错误。
在调用-controllerDidChangeContent:期间,NSFetchedResultsController的委托捕获到exception。
无效的更新:无效的部分数量。
更新(7)之后,表格视图中包含的部分数量必须等于更新前的表格视图中包含的部分数量(6),加上或减去插入或删除的部分数目(0插入,0删除)。 与userInfo(null)

我的应用程序有多个部分tableView。 只有当我尝试插入新节logging时才会出现错误。 如果该部分已经存在,则不会发生错误。

相关的代码如下。

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; DLog(@"newIndexPath %@",newIndexPath); switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { /*NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { DLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self.tableView reloadData];*/ [self.tableView endUpdates]; } 

此外,我把sectionNameKeyPath在fetchResultsController。 实际核心数据中不存在年份列,这是扩展列。

  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"year" cacheName:@"Master"]; 

我可以通过以下更改来解决错误。 只是为了一个人在相同的情况。

我注释了下面这行。

 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; DLog(@"newIndexPath %@",newIndexPath); switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

我添加了以下行。

  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { // In the simplest, most efficient, case, reload the table view. NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { DLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [self.tableView reloadData]; }