与 iCloud 连接的 iOS 应用程序崩溃

发布于 2024-12-12 22:08:42 字数 1867 浏览 1 评论 0原文

我的应用仅使用一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思碎 2024-12-19 22:08:43

您的崩溃表明您启动了一个 -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 back 3 when it expected 2. It sounds like you're not updating your data source properly.

鱼窥荷 2024-12-19 22:08:43

即使记录下来,也会发生一些事情。我的意思是,它坏了,所以它显然无法正常工作,对吗?
一些想法:

  1. 也许您用来返回 numberOfRowsInSection 的方法
    设置不当。
  2. 也许 cellForRowAtIndexPath 错误地查看了错误的列表。
  3. 您可以在 for 循环中删除并刷新显示。

您的错误明确指出它正在尝试在某个时刻查找索引 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:

  1. Perhaps the method you are using to return the numberOfRowsInSection
    is set up improperly.
  2. Perhaps cellForRowAtIndexPath is mistakenly looking at the wrong list.
  3. You could be deleting and refreshing the display in a for loop.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文