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

查看:15
本文介绍了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 和 Martin)都帮助我找到了它.Martin 得到了答案,因为这些解释帮助我更好地理解了...

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...

推荐答案

不要打电话

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

如果您使用获取的结果控制器.只用

if you use a fetched results controller. Only delete the object with

[self.managedObjectContext deleteObject:sd];

获取的结果控制器委托方法 didChangeObject: 然后被自动调用,并调用 deleteRowsAtIndexPaths:.

The fetched results controller delegate method didChangeObject: is then called automatically, and that calls deleteRowsAtIndexPaths:.

因此,在您的情况下,该行被删除了两次,这导致了异常.

So in your case, the row was deleted twice, and that caused the exception.

请注意,这里不需要 beginUpdates/endUpdates.

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

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

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