【问题标题】:addNotificationBlock { RealmCollectionChange } crashes in UITableViewUITableView 中的 addNotificationBlock { RealmCollectionChange } 崩溃
【发布时间】:2016-06-24 16:34:52
【问题描述】:

我正在使用 Realm for Swift 并将数据加载到 UITableView 中。当我进入屏幕时,大约有 200 个数据对象正在逐渐下载,因此在显示 tableview 之后,在我的测试中发生了很多插入 UITableView 的操作。我正在使用 Realm 示例尽可能接近地添加带有 RealmCollectionChange 的NotificationBlock,并且在此过程中偶尔会发生两次单独的崩溃。

  1. *** 由于未捕获的异常“RLMException”而终止应用程序,原因:“只能从运行循环中添加通知块。”

即使我特意从 ViewController 类的主线程中提取所有数据,也会发生这种崩溃。

  1. *** -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.30.14/UITableView.m:1720 **** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (27) 必须等于更新前该节中包含的行数 (17),加上或减去从该节插入或删除的行数(9 插入,0 删除),加上或减去移入或移出该节的行数(0 移动入,0 移出)。'

这种崩溃是在我更换后才开始发生的

tableView.reloadData()

与

tableView.beginUpdates()
        tableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        tableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        tableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
          withRowAnimation: .Automatic)
        tableView.endUpdates()

在我的 addNotificationBlock 的 .Update() 部分中

Realm 是否有一些我遗漏的东西导致了这种情况?我怀疑是因为我没有完全理解这个库的内部机制。

这是我的参考代码:

self.exhibits = DataManager.getAllExhibitsSorted("id")
token = self.exhibits?.addNotificationBlock { (changes: RealmCollectionChange) in
  switch changes {
  case .Initial(_):

    self.exhibits = DataManager.getAllExhibitsSorted("id")
    self.exhibitListTableView.reloadData()
    break
  case .Update(_, let deletions, let insertions, let modifications):

    // Query results have changed, so apply them to the UITableView
    self.exhibitListTableView.beginUpdates()
    self.exhibitListTableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) },
      withRowAnimation: .Automatic)
    self.exhibitListTableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) },
      withRowAnimation: .Automatic)
    self.exhibitListTableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) },
      withRowAnimation: .Automatic)
    self.exhibitListTableView.endUpdates()

    break
  case .Error:
    NSLog("Error in notificationBlock")
    break
  }
}

【问题讨论】:

    标签: ios swift uitableview realm


    【解决方案1】:

    听起来你在这里可能有点过于复杂了。

    领域Results 对象是实时和自动更新的。这意味着对其底层对象所做的更改会在主运行循环的下一次迭代中自动更新,因此无需对它们执行手动重新获取。在您的代码中,您在生成令牌后在.Initial 更改通知中重新分配self.exhibits,这可能会导致您的一些问题。如果您删除该行,它应该会继续工作。

    我建议检查您的代码,并确保 self.exhibits 只被分配一次,并且更改通知方法仅应用于那个。

    如果这不能解决问题,请告诉我。

    【讨论】:

    • 呃。大部分时间都设法解决了它,但是当我在下载过程中打开和关闭 wifi 时,我仍然收到 NSInternalInconsistencyException 。这可能是 notificationBlock.Update 和 self.exhibits 中存在的值之间的竞争条件。即使只是有关如何捕获该错误的一些帮助也可能就足够了。 .Update 中的所有内容都不应该引发错误,所以我不能用 do-catch 包围它。
    • 值得我遇到完全相同的问题。你有没有解决过这个问题?这似乎是 Realm 内部某个地方的竞争条件。我正在使用领域文档中显示的代码 100%...
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2011-09-27
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2011-08-08
    相关资源
    最近更新 更多