gpt4 book ai didi

ios - addNotificationBlock { RealmCollectionChange } 在 UITableView 中崩溃

转载 作者:可可西里 更新时间:2023-11-01 01:35:37 33 4
gpt4 key购买 nike

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

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

即使我特意从我的 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
}
}

最佳答案

听起来你可能把这里的事情复杂化了一点。

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

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

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

关于ios - addNotificationBlock { RealmCollectionChange } 在 UITableView 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38018213/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com