NSFetchedResultsController:多个 FRC,更新时委托错误 [英] NSFetchedResultsController: Multiple FRCs, Delegate Error when Updating

查看:13
本文介绍了NSFetchedResultsController:多个 FRC,更新时委托错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用 FRC,按 startDateNSDate 属性对 Section's 进行排序,但需要 今天的日期Section出现在Upcoming日期Section之前.

Objective: Using FRC, sort Section's by startDate, an NSDate attribute, but want Today's date Section to appear before Upcoming dates Section.

我使用瞬态属性 sectionIdentifier 遵循 Apple 的代码.Apple 的示例代码. 并开始于这个项目第一:OneFRC

I followed Apple's code using a transient property sectionIdentifier. Apple's sample code. and started with this project first: OneFRC

我很快意识到仅使用一个 FRC 可能无法做到这一点(我可能错了).

I soon realized that this may not be possible with just one FRC (I could be wrong).

接下来,我决定用 3 个 FRC 尝试一下:三FRC.

TableView sections 现在出现在我想要的顺序中:

TableView sections now appears in the Order that I want:

第 0 部分:今天

第 1 部分:即将推出

第 2 部分:过去

但是,添加数据会触发 FRC 委托,并且出现以下错误:

However, adding data triggers FRC delegates, and I get the following error:

CoreData: error: Serious application error.  An exception was caught from the 
delegate of NSFetchedResultsController during a call to 
-controllerDidChangeContent:.  Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (4) must be 
equal to the number of rows contained in that section before the update (3), plus 
or minus the number of rows inserted or deleted from that section (0 inserted, 
0 deleted) and plus or minus the number of rows moved into or out of that section 
(0 moved in, 0 moved out). with userInfo (null)

同样,我很想用 1 个 FRC 来实现我的目标,但我似乎不知道如何实现.

Again, I would love to be able to accomplish my objective with 1 FRC, but I can't seem to figure out how.

我已经尝试解决这个问题 4 天了!如果这个问题在 SO 上没有得到解决,我想我可以联系 Apple 以获得开发人员支持.如果我这样做了,我会在此处发布决议,以便其他人受益.

I have been trying to resolve this for 4 days now! If this issue doesn't get resolved on SO, I think I may reach out to Apple for Developer support. And in the event that I do, I'll post the resolution here so others can benefit.

项目在 Github 上可用:

一个 FRC

三个 FRC

编辑

感谢@blazejmar,我摆脱了rows 错误.但是,现在当我尝试添加 sections 时出现错误.

Thanks to @blazejmar, I was able get rid of the rows error. However, now I get an error when I attempt to add sections.

2014-11-03 16:39:46.852 FRC[64305:60b] 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 (2) must be 
equal to the number of sections contained in the table view before the update (1), 
plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). 
with userInfo (null)

在三个 FRC 中重现错误的步骤:

Steps to reproduce the error in Three FRC:

1. Launch App -> 
2. Tap Generate Data -> 
3. Tap View in FRC -> 
4. Tap back to the RootVC ->
5. Change the system date to a month from Today -> 
6. Tap View in FRC and only one section `Past` should appear. -> 
7. Tap `Add Data`.  
8. The error should appear in the log. 

推荐答案

在你的 ThreeFRC 项目中存在一些问题:

In your ThreeFRC project there are some issues:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
    self.numberOfSectionsInTV = 0;
    [self fetchData];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView reloadData];
    [self.tableView endUpdates];
}

您不应在 FRC 委托中使用 fetchData.方法以正确的顺序(更新前、更新期间和更新后)调用,因此在回调内部您具有一致的上下文状态.此外,在 endUpdates 之前使用 reloadData (它应用您之前提供的所有更改)也不是最好的主意,并且 reloadData 正在擦除所有内容并从头开始构建它.这很可能是导致崩溃的原因.

You shouldn't use fetchData inside FRC delegate. Methods are called in proper order (before, during and after update) so inside callbacks you have consistent state of context. Also it's not the best idea to use reloadData before endUpdates(it's applying all changes you provided earlier) and reloadData is erasing everything and building it from scratch. This is most likely causing the crash.

我发现的其他可能有问题的事情是处理更新.如果您有 3 个没有节的独立 FRC,您将不会在 FRC 委托中获得节更新回调.但是,如果某些对象出现在其中一个 FRC 中,那么您应该检测到并手动插入它们.

Other thing I've spotted that may be buggy is handling of updates. If you have 3 separate FRC without sections you won't get section update callback in FRC delegate. But if some objects appear in one of the FRC's then you should detect that and manually insert them.

在 controllerDidChangeContent 中只使用 reloadData 就足够了,但这不是最好的解决方案,因为你不会得到任何动画.正确的方法是处理所有情况:从其中一个 FRC 中删除所有对象(然后从 TableView 中手动删除节),将第一个对象插入 FRC(然后您应该在适当的 indexPath 创建新节).

Using just reloadData in controllerDidChangeContent would be enough, but this isn't the best solution, as you won't get any animations. The proper way would be to handle all the cases: deleting all objects from one of FRCs (and then deleting section manually from TableView), inserting first object into FRC (then you should create new section at proper indexPath).

这篇关于NSFetchedResultsController:多个 FRC,更新时委托错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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