与 iCloud 连接的 iOS 应用程序崩溃
我的应用仅使用一个 managedObjectContext
并设置为与 iCloud 同步核心数据更改,并且我使用 NSFetchedResultsController
为我的 UITableView
获取数据。当我尝试编辑(重新排序)表格单元格时出现问题。
想象一下这个场景:
我执行 [tableView setEditing:YESanimated:YES];
我将手指按住一行,然后开始在 tableView
中移动它(重新排序)。与此同时,我在 mac 应用程序中删除一行,并保存更改。
iOS 应用程序了解新的更改,我的 fetchedResultsController
尝试调用 [tableView beginUpdates]
...并从 tableView
中删除该行当它正在被编辑的时候。
我不知道它是否应该这样工作,但它崩溃了。
你会如何尝试解决这个问题?
日志:
***[3758:707] asynchronously added persistent store!
***[3758:707] numberOfRowsInSection:3
***[3758:707] numberOfRowsInSection:2
***[3758:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1912.3/UITableView.m:1046
***[3758:707] 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 (3) 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, 1 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)
***[3758:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]'
前两个日志位于 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法中,并记录我返回的内容。我发现第一次返回3,第二次返回2!
编辑:现在我在想......它应该这样工作,还是我必须找到另一种方法来实现它?当您拖动单元格并调用 [tableView moveRowAtIndexPath:toIndexPath:]; 时会发生什么? ?
My app uses just one managedObjectContext
and is set up to sync coredata changes with iCloud, and I'm using NSFetchedResultsController
to fetch data for my UITableView
. The problem occurs when I try to edit (reorder) the table cells.
Imagine this scenario:
I execute [tableView setEditing:YES animated:YES];
I hold down my finger on a row, and start to move it around the tableView
(reorder). In the meantime I delete a row in the mac app, and save the changes.
iOS app gets to know about the new changes and my fetchedResultsController
tries to call [tableView beginUpdates]
... and remove the row from the tableView
while it's being edited.
I don't know if it should work this way, but it crashes.
How would you try to solve this?
Log:
***[3758:707] asynchronously added persistent store!
***[3758:707] numberOfRowsInSection:3
***[3758:707] numberOfRowsInSection:2
***[3758:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1912.3/UITableView.m:1046
***[3758:707] 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 (3) 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, 1 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)
***[3758:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]'
The first two logs are in the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method and log what i'm returning. I found out that the first time I returned 3, and the second time it was 2!
EDIT: And now im thinking ... should it work this way, or I have to find another way to make it ? What should happen when you're dragging a cell, and call [tableView moveRowAtIndexPath:toIndexPath:]; ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的崩溃表明您启动了一个
-beginUpdates
块,删除了一行,然后调用了-endUpdates
,但是当 tableview 向其数据源询问该行的行数时,部分,当它期望2
时,它被交还给3
。听起来您没有正确更新数据源。Your crash says that you started a
-beginUpdates
block, deleted a row, and then called-endUpdates
, but when the tableview asked its data source for the number of rows in that section, it was handed back3
when it expected2
. It sounds like you're not updating your data source properly.即使记录下来,也会发生一些事情。我的意思是,它坏了,所以它显然无法正常工作,对吗?
一些想法:
设置不当。
您的错误明确指出它正在尝试在某个时刻查找索引 2.. 处的项目。没有源代码,任何人都可以猜测为什么它看起来如此。我在这些事情上遇到了一些问题,最终总是我的错。解决这个问题的一种劳动密集型但非常简单的方法是放置大量 NSLog 语句,这样您就可以跟踪 get 何时被调用。我猜您是在错误的时间从数组中删除了对象。而且很容易犯错误。
Even with logging it, something is happening. I mean, it's breaking so it's obviously not working correctly right?
Some ideas:
is set up improperly.
Your error explicitly states that it's trying to find item at index 2.. at some point. Why it's looking there is anyones guess without the source code. I had some issues with these things and it was always my fault in the end. A labor intensive but pretty foolproof way to figure this out is to put a shit-load of NSLog statements so you can trace what get's called when. I'd guess that you are deleting the the object from the array at the incorrect time. And very easy mistake to make.