首页 文章

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

提问于
浏览
1

你好我现在堆叠了下面的错误 .

CoreData:错误:严重的应用程序错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . 无效更新:无效的节数 . 更新后的表视图中包含的节数(7)必须等于更新前的表视图中包含的节数(6),加上或减去插入或删除的节数(插入0,0删除) . 用户信息(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中 . 年份列在真实核心数据中不存在,它是扩展列 .

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

1 回答

  • 1

    我可以通过以下更改来解决错误 . 仅适用于处于相同情况的人 .

    我评论了以下内容 .

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

相关问题