【发布时间】:2016-06-24 16:34:52
【问题描述】:
我正在使用 Realm for Swift 并将数据加载到 UITableView 中。当我进入屏幕时,大约有 200 个数据对象正在逐渐下载,因此在显示 tableview 之后,在我的测试中发生了很多插入 UITableView 的操作。我正在使用 Realm 示例尽可能接近地添加带有 RealmCollectionChange 的NotificationBlock,并且在此过程中偶尔会发生两次单独的崩溃。
- *** 由于未捕获的异常“RLMException”而终止应用程序,原因:“只能从运行循环中添加通知块。”
即使我特意从 ViewController 类的主线程中提取所有数据,也会发生这种崩溃。
- *** -[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