问题描述
目标:使用FRC,通过 startDate 对节的进行排序, $ c> NSDate 属性,但希望今天的日期 code>即将到期日期节。
我按照Apple的代码使用了一个暂时属性 sectionIdentifier 。 ,并以此项目首先:
我很快意识到这可能不可能只用一个FRC(我可以错了)。
接下来,我决定使用3个FRC 尝试一下:。
TableView 节现在出现在我想要的订单中:
第0部分:今天
第1部分: $ c>
第2节:过去
但是,添加数据会触发FRC委托,我会收到以下错误:
CoreData:错误:严重的应用程序错误。在调用
-controllerDidChangeContent:的过程中,NSFetchedResultsController的
委托捕获了一个异常。无效的更新:部分0中的行数无效。
更新(4)后,现有节中包含的行数必须为
,等于更新前该节中包含的行数3),加上
或减去从该部分插入或删除的行数(插入0,删除
0),并加上或减去移入或移出该部分的行数
(0移入,0移出)。 with userInfo(null)
再次,我希望能够完成我的目标1 FRC ,但我似乎无法弄清楚如何。
我一直在尝试解决这个问题4天了!如果这个问题不能解决SO,我想我可以联系苹果的开发商支持。如果我这样做,我会在这里发布的决议,以便其他人可以受益。
$ b
//github.com/KausiAhmed/FRCrel =nofollow> 一个FRC
/ strong>
感谢@blazejmar,我能够摆脱行错误。但是,现在我尝试添加节时出现错误。
2014-11-03 16:39:46.852 FRC [64305:60b] CoreData:error:严重的应用程序错误。
在对_controllerDidChangeContent:
调用期间,NSFetchedResultsController的委托捕获到异常。无效的更新:版面号无效。
更新(2)后表格视图中包含的节数必须为
,等于更新前的表视图中包含的节数(1),
加或减插入或删除的节数(插入0个,删除0个)。
with userInfo(null)
在Three FRC中重现错误的步骤:
1。启动应用程序 - >
2.点击生成数据 - >
3.点击FRC中的View - >
4.点击返回到RootVC - >
5.将系统日期从Today更改为一个月 - >
6.点击FRC中的View,只出现一个'Past'部分。 - >
7.点击`添加数据'。
8.错误应该出现在日志中。
在您的ThreeFRC项目中有一些问题: p>
- (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正在擦除一切,从头开始构建。这很可能导致崩溃。
我发现其他事情可能是错误的处理更新。如果你有3个单独的FRC没有部分,你不会在FRC委托中获得部分更新回调。但是如果一些对象出现在一个FRC中,那么你应该检测到并手动插入它们。
在controllerDidChangeContent中使用reloadData就足够了,但这不是最好的解决方案,因为你不会得到任何动画。正确的方法是处理所有的情况:从一个FRC中删除所有对象(然后从TableView手动删除部分),将第一个对象插入到FRC中(然后你应该在适当的indexPath中创建新的部分)。
Objective: Using FRC, sort Section's by startDate, an NSDate attribute, but want Today's date Section to appear before Upcoming dates Section.
I followed Apple's code using a transient property sectionIdentifier. Apple's sample code. and started with this project first: OneFRC
I soon realized that this may not be possible with just one FRC (I could be wrong).
Next, I decided to take a stab at this with 3 FRCs: ThreeFRC.
TableView sections now appears in the Order that I want:
Section 0: Today
Section 1: Upcoming
Section 2: Past
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)
Again, I would love to be able to accomplish my objective with 1 FRC, but I can't seem to figure out how.
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.
Projects are available on Github:
EDIT
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)
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.
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]; }
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.
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.
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,更新时委派错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!