【问题标题】:Is it possible to use Realm object server real time sync without indices?是否可以在没有索引的情况下使用 Realm 对象服务器实时同步?
【发布时间】: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


【解决方案1】:

所有领域查询都返回Results,它是auto-updating 容器类型,因此您无需再次进行查询。您可以为任何特定的Result 设置notification handler,并在更改此集合以更新映射模型时收到通知。

【讨论】:

  • 好的。这会很困难,因为我的架构不是反应式的。我正在使用无状态提供程序,我通过回调检索数据。必须弄清楚一些事情,可能我会直接在控制器中使用Result。只是为了确认一下,如果我有一个包含多个过滤器/谓词的查询,我收到的索引指的是这个特定的结果,即包括所有过滤器,对吧?
猜你喜欢
  • 1970-01-01
  • 2013-01-03
  • 2018-09-15
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多