【发布时间】:2016-12-04 19:55:38
【问题描述】:
我正在研究 Realm 的“任务”示例应用中实现的实时同步。
特别是这个块:
private func setupNotifications() -> NotificationToken {
return parent.items.addNotificationBlock { [unowned self] changes in
switch changes {
case .Initial:
// Results are now populated and can be accessed without blocking the UI
self.viewController.didUpdateList(reload: true)
case .Update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
self.viewController.tableView.beginUpdates()
self.viewController.tableView.insertRowsAtIndexPaths(insertions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic)
self.viewController.tableView.deleteRowsAtIndexPaths(deletions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic)
self.viewController.tableView.reloadRowsAtIndexPaths(modifications.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .None)
self.viewController.tableView.endUpdates()
self.viewController.didUpdateList(reload: false)
case .Error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError(String(error))
}
}
}
基本上,更改是使用索引来传达的。只需使用这些索引访问底层模型/领域对象,即可更新接口。
现在我的架构似乎与此不兼容。我有一个专用的数据库层(其中领域是一个实现),我在后台线程中加载领域对象并映射到普通模型对象。这样我就可以将我的代码与数据库实现分离,并且可以使用不可变模型。
我不确定在这种情况下如何处理索引。看起来我应该记住原始查询,再做一次,然后使用这些索引访问我需要的条目?听起来效率很低……
此外,我不知道索引如何处理特定查询,例如“在字段 y 中具有状态 x 的所有项目” - 我收到的索引是否引用此特定查询?
这里推荐的方法是什么?
编辑:只是为了添加一些额外的评论,我使用自定义服务器和 websockets 实现了自己的同步功能,并且我使用语义键而不是索引(有时我什至发送了完整的对象以避免查询数据库)。这样我就不必处理基于索引的访问可能导致的不一致。想知道类似的事情是否可能或计划在某个时候使用 Realm 同步。
附:我打算切换到 Realm 同步,因为我的自定义服务器没有经过很好的测试并且很难维护。我希望这是可能的。
【问题讨论】:
-
推荐的方法是将零拷贝数据库用作零拷贝数据库,而不是将所有内容映射出来 - 但您可以在official documentation中阅读更多相关信息
-
嗯,将接口代码与 DB 实现分离是让我首先切换到领域的原因。当然它有缺点,我知道。
标签: ios swift realm realm-object-server