NSFetchedResultsController:删除1行会导致应用程序崩溃 [英] NSFetchedResultsController: delete 1 row crashes the app

查看:121
本文介绍了NSFetchedResultsController:删除1行会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从NSFetchedResultsController提供的UITableView中删除一行会导致我的应用程序崩溃。

Deleting a row from a UITableView fed by an NSFetchedResultsController causes my app to crash.

错误是:

* - - [UITableView _endCellAnimationsWithContext:]中的断言失败,/SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330

*
应用程序由于未捕获异常NSInternalInconsistencyException,原因:无效的更新:第0节中的行数不正确。更新(1)后现有段中包含的行数必须等于该段中包含的行数在更新(1)之前,加或减从该部分插入或删除的行数(0插入,1删除)和加或减移动或移出该部分的行数(0移入,0移出)。'

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330
*
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我只想滑动到删除。我的删除代码是这样的:

I want only swipe-to-delete. My deletion code goes like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{  
    [self.tableView beginUpdates];  
    SequenceData *sd = [self.fetchedResultsController objectAtIndexPath:indexPath];  
    [self.managedObjectContext deleteObject:sd];  
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];  
    [self.tableView endUpdates];  
}

我的NSFetchedResultsController设置和Ray Wanderlich的教程一样( http://www.raywenderlich.com/999/core -data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

My NSFetchedResultsController is set up just like in Ray Wanderlich's tutorial (http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller)

行数由以下因素决定:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    
    id sectionObjects = [[self.fetchedResultsController sections] objectAtIndex:section];  
    NSInteger nbObjects = [sectionObjects numberOfObjects];  
    return nbObjects;  
}

看起来像抓取不更新(行数不变) 。我需要抓取自己(这不是由抓取控制器照顾)?

It looks like the fetch is not updating (number of rows does not vary). Do I need to fetch myself (isn't this taking care of by the fetch controller)?

这里显然有一些基本的东西。不要犹豫,提出基本的答案。

There is obviously something basic I am missing here. Don't hesitate to suggest basic answers.

我首先使用控制器实现了这个:didChangeObject:...方法。错误是一样的,但有些细节不同。

I first implemented this with the controller: didChangeObject: ... method. Error was the same, but some details differed.

编辑
我相信我的问题已修复。这两个答案(从CX和马丁)帮助我找到它。马丁得到了答案,因为解释帮助我更好地理解...

Edit I believe my problem is fixed. Both answers (From CX and Martin) helped me find it. Martin got the answer because of the explanations that helped me understand a little bit better...

推荐答案

p>

Don't call

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

只能使用

[self.managedObjectContext deleteObject:sd];

获取的结果控制器委托方法 didChangeObject:

注意,你不需要 beginUpdates / endUpdates 这里。

Note that you don't need beginUpdates/endUpdates here.

这篇关于NSFetchedResultsController:删除1行会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆