更新CoreData和UINavigationController堆栈

时间:2014-04-26 09:16:55

标签: ios core-data uinavigationcontroller nsfetchedresultscontroller

我的故事板看起来像这样:

摘要列表 - >递归表视图 - >物品视图

它由导航控制器管理,最好的部分工作正常,我期待。我可以在摘要列表中编辑没有问题的数据:

Summary List -> Recursive Table View -> Item View
  |
  | <- (make a change here, use beginUpdate and endUpdate)
  |
Core Data

这一点工作正常......

当应该进入项目视图时,核心数据对象会更新,然后项目视图会出现在下一次加载摘要列表中。

但是,由于某些原因,摘要列表仍然在后台刷新,我假设因为它在导航层次结构中,因此当我在Item View板上更新CoreData对象时,我得到了经典&# 39; CoreData:错误:严重的应用程序错误。在调用-controllerDidChangeContent&#39;期间,从NSFetchedResultsController的委托中捕获到异常。 - 通常我会通过确保我调用beginUpdate和endUpdate来解决这个问题,但是如果我和tableView之间可能有很多表视图,我该怎么办呢?

这个位不起作用:

Summary List -> Recursive Table View -> Item View
  |                                         |
  |                                         | > (a change here modifies the core
  |                                              data database, Summary List is
  |                                              still somehow loaded and error)
  |
Core Data

完整的错误是经典的错误&#39;:

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 1.
The number of rows contained in an existing section after the update
(0) must be equal to the number of rows contained in that section
before the update (1), 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).

2 个答案:

答案 0 :(得分:1)

如果您以标准方式使用获取的结果控制器设置表格,我很确定表格是否在屏幕上并不重要。听起来好像正在进行一些不寻常的更新和/或您没有正确使用获取的结果控制器作为数据源。

如果您遇到问题,可以通过将获取的结果控制器的委托设置为nil来关闭跟踪。当视图离屏时尝试此操作,并在它返回时恢复(并重新加载表)。

请注意,由于缺少begin和endUpdates调用,您看到的异常不是。如果他们失踪了,你就不会得到例外。它们正在发生,但在那里,插入/删除和最终行数之间没有匹配。在beginUpdates之前,您在第1部分中有一行,在您没有之后,并且在您不删除任何内容时。

答案 1 :(得分:0)

您需要实现获取的结果控制器委托方法。在委托回调中,请确保删除并添加适当数量的单元格和节。然后这个错误肯定会消失。