NSFetchedResultsController让我发疯 [英] NSFetchedResultsController is driving me crazy

查看:102
本文介绍了NSFetchedResultsController让我发疯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NSFetchedResultsController从1个月开始构建应用程序,我正在测试3.1.2 SDK上的应用程序。问题是我一直在我的应用程序中使用NSFetchedResultsController并正在使用SDK的3.1.2版本,现在我的客户说我应该使它与3.0版本兼容,截止日期几乎就在那里。

i've been building an app since 1 month using NSFetchedResultsController and i was testing the app on the 3.1.2 SDK. The poblem is that i've been using NSFetchedResultsController everywhere in my app and was working on the 3.1.2 version of the SDK, now my client say that i should make it compatible with the 3.0 version and the deadline is almost there.

但是每当我更改控制器处理的对象时崩溃,应用程序崩溃都会出现非常奇怪的错误。

But is crashing everytime i change an object handled by the contoller, the application is crashing with very weird errors.

删除部分中的最后一个对象时以及当一个更改使一个对象爱到另一个部分时出现问题。

The problem occure when removing the last object in a section and when a change make an object love to another section.

我一直在使用更多iPhone中的示例代码3开发解决iPhone SDK 3由戴夫马克和杰夫拉马奇。我还在链接文字中添加了一些更改

I've been using a sample code from "More iPhone 3 Development Tackling iPhone SDK 3" by Dave Mark and Jeff LaMarche. I've also included some changes from link text

以下是应用程序崩溃时控制台的示例输出。

Here is a sample output of the console when the application is crashing.

** *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:无效的节数。更新后的表视图中包含的节数(1)必须等于更新前的表视图中包含的节数(2),加上或减去插入或删除的节数(插入2个,0删除)。'
2010-03-14 16:23:29.758 Instaproofs [5879:207] Stack:(
807902715,
7364425,
807986683,
811271572 ,
815059090,
815007323,
211023,
4363331,
810589786,
807635429,
810579728,
3620573,
3620227,
3614682,
3609719,
27337,
810595174,
807686849,
807683624,
839142449,
839142646,
814752238

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (2 inserted, 0 deleted).' 2010-03-14 16:23:29.758 Instaproofs[5879:207] Stack: ( 807902715, 7364425, 807986683, 811271572, 815059090, 815007323, 211023, 4363331, 810589786, 807635429, 810579728, 3620573, 3620227, 3614682, 3609719, 27337, 810595174, 807686849, 807683624, 839142449, 839142646, 814752238 )

如果我知道NSFetchedResultsController是如此错误,我将永远不会使用它。

If i knew that NSFetchedResultsController is so buggy, i would never used it.

所以基本上我需要我的NSFetchedResultsControllerDelegate才能在3.0及以上的SDK上正常工作。

So basicaly i need the my NSFetchedResultsControllerDelegate to work fine on the 3.0 and above SDKs.

如果不是帮我弄清楚我做错了什么。

It would be life saver if someone help me figure out what i'm doing wrong.

推荐答案

从你的错误消息中可以看出,你正在将部分插入到表应该删除它们。你的tableView dataSource仅在更新后提供一个部分,即使你已经告诉tableView预计总共有四个部分。

It appears from your error messages that you are inserting sections into the table when you should be deleting them. Your tableView dataSource is only supplying one section after the updates even though you have told the tableView to expect a total of four sections.

我不认为这是一个案例NSFetchedResultsController是错误的,但在简单的用例之外实现它是棘手的。您的崩溃几乎肯定是由于您的控制器而发生的:didChangeObject:atIndexPath:forChangeType:newIndexPath委托方法。成功实现此方法的关键(至少在我的经验中)是要记住changeTypes是对象和索引路径驱动的。这使得更新和移动在概念上变得棘手。

I don't think this is a case of NSFetchedResultsController being buggy, but rather that it is tricky to implement outside of simple use cases. Your crashes are almost certainly occurring as a result of your controller:didChangeObject:atIndexPath:forChangeType:newIndexPath delegate method. The key to successfully implementing this method (at least in my experience) is to keep in mind that the changeTypes are both object and indexPath driven. This makes "updates" and "moves" tricky conceptually.

考虑更改托管对象以使其在新的sectionNameKeyPath下排序的情况。从概念上讲,我们认为对象已经移动到一个新的部分,因为fetchedResultsController现在在tableView的新标题下对其进行排序。但是,如果对象的indexPath没有改变,那么fetchedResultsController认为这是更新而不是移动。

Consider the situation where a managed object is changed such that it sorts under a new sectionNameKeyPath. Conceptually, we think of the object as having "moved" to a new section since the fetchedResultsController now sorts it under a new heading in the tableView. If the object's indexPath does not change, however, the fetchedResultsController considers this an "update" and not a "move".

更糟糕的是,即使托管对象是如果仍然保留了相同的indexPath,则fetchedResultsController中的其他对象现在可能具有新的indexPath,因为它们被更改所影响。这意味着您必须在委托方法的更新部分中手动处理部分插入和部分删除。类似的问题需要在你的委托方法的移动部分解决。

What's worse is that even though the managed object that changed still retains the same indexPath, other objects in the fetchedResultsController may now have new indexPaths because they were bumped around by the change. This means that you will have to manually handle section insertions and section deletions in the "update" section of your delegate method. Similar problems will need to be addressed in the "move" section of your delegate method.

没有用这么多的话来解释它,LaMarche的修正试图解决这个问题。以通用方式容纳尽可能多的用例的问题。通过尝试了解与您的用例相关的问题,您可以显着降低LaMarche使用的代码的复杂性。专注于代理方法的更新和移动部分,因为这些是您问题的最可能的罪魁祸首。

Without trying to explain it in so many words, LaMarche's fix is attempting to address this problem in a generic way that accommodates as many use cases as possible. By trying to understand the problem as it pertains to your use case, you may be able to significantly reduce the complexity of the code that LaMarche uses. Focus specifically on the "update" and "move" sections of your delegate method, as these are the most likely culprits for your problem.

这篇关于NSFetchedResultsController让我发疯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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