Ios addNotificationBlock{RealmCollectionChange}在UITableView中崩溃
我正在使用Realm进行Swift,并将数据加载到UITableView中。当我进入屏幕时,大约有200个数据对象正在逐渐下载,因此在我的测试中,在tableview显示之后,会有大量的数据插入到UITableView中。我正在使用Realm示例尽可能紧密地添加NotificationBlock和RealmCollectionChange,并且在这个过程中偶尔会发生两个单独的崩溃Ios addNotificationBlock{RealmCollectionChange}在UITableView中崩溃,ios,swift,uitableview,realm,Ios,Swift,Uitableview,Realm,我正在使用Realm进行Swift,并将数据加载到UITableView中。当我进入屏幕时,大约有200个数据对象正在逐渐下载,因此在我的测试中,在tableview显示之后,会有大量的数据插入到UITableView中。我正在使用Realm示例尽可能紧密地添加NotificationBlock和RealmCollectionChange,并且在这个过程中偶尔会发生两个单独的崩溃 ***由于未捕获的异常“RLMEException”而终止应用程序,原因:“只能从runloops中添加通知块。”
tableView.reloadData()
与
在我的addNotificationBlock的.Update()部分
我想这是因为我没有完全理解这个库的内部机制
以下是我的代码供参考:
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
结果
对象是实时和自动更新的。这意味着在主运行循环的下一次迭代中,对其基础对象所做的更改将自动更新,因此无需对其执行手动重新获取。在那里的代码中,您在初始
更改noti中分配了self.expensions
例如,在生成令牌之后,这可能会导致您在此处遇到一些问题。如果删除该行,它将继续工作
我建议您仔细检查代码,确保只分配一次self.exhibies
,并且更改通知方法仅应用于该代码
如果这还不能解决问题,请告诉我。Ugh。这在大多数情况下都能解决问题,但当我在下载过程中打开和关闭wifi时,仍然会出现NSInternalInconsistency异常。这可能是notificationBlock.Update和self.exhibies中存在的值之间的竞争条件。甚至只是一些关于如何捕获该异常的帮助t错误可能就足够了。更新中的任何内容都不应该抛出错误,所以我不能用do-catch来包围它。值得一提的是,我遇到了完全相同的问题。你解决过这个问题吗?这似乎是领域内的某个竞争条件。我正在使用领域文档中显示的100%代码。。。
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
}
}