ios - controllerDidChangeContent 核心数据严重应用错误

标签 ios objective-c uitableview core-data nsfetchedresultscontroller

您好,我现在遇到以下错误。

CoreData: error: Serious application error.
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
Invalid update: invalid number of sections.
The number of sections contained in the table view after the update (7) must be equal to the number of sections contained in the table view before the update (6), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)

我的应用程序有多个部分 tableView。 并且仅当我尝试插入新的部分记录时才会发生错误。 如果该部分已经存在,则不会发生错误。

相关代码如下。

- (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 上。 实际核心数据中不存在year列,是extension列。

    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];
 }

关于ios - controllerDidChangeContent 核心数据严重应用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601064/

相关文章:

ios - 更改 App Delegate 中导航栏的外观是否会覆盖嵌入式导航栏?

ios - 我如何解除 segue 回退?

iphone - 为 UITableViewController 子类调用 loadView

ios - UITableView 自定义单元格 - 如何将 cornerRadius 保持在编辑模式

ios - apple In-App Purchase 服务器回调 JSON 不包含 “latest_receipt_info” 键?

ios - NSPhotoLibraryAddUsageDescription 错误

ios - 自定义类不显示在属性检查器中

ios - 带有图像背景的 iOS 7 中的 UITableViewCell 问题

ios - 登录 Realm Mobile Platform 后弹出 View Controller

ios - 如何知道点击 map 时已经创建了一个圆圈并显示警告以在 MAPKIT 中删除和编辑该圆圈?