【发布时间】:2018-05-29 21:26:08
【问题描述】:
我正在尝试了解解决冲突在 firebase 中的工作方式,我需要一些帮助。
假设我已经将 json 对象保存在 firebase 的一个节点中 实时:
{
"shape": "rectangle",
"stroke": 10,
"color": "black"
}
我已经定义了一个测试页面,它可以读取这些数据并显示,还可以实时监听节点上发生的变化。我添加了一项更新数据的规定,最终仅更新特定的键值。
用例示例
client 1 - loads the page
data - {"shape": "rectangle", "stroke": 10, "color": "black"}
client 2 - loads the page
data - {"shape": "rectangle", "stroke": 10, "color": "black"}
client 2 goes offline
client 2 updates stroke value to 20
data - {"shape": "rectangle", "stroke": 20, "color": "black"}
* data is yet to sync to the server
client 1 makes a change after client 2 has already done with its changes and changes stroke to 5
data - {"shape": "rectangle", "stroke": 5, "color": "black"}
* data gets synced to the server immediately
client 2 comes online and pushes its changes and overrides the changes made by client 1
data - {"shape": "rectangle", "stroke": 20, "color": "black"}
理想情况下,由于客户端 1 的更改晚于客户端 2,因此客户端 1 的更改应在客户端 2 数据同步时保留。
如果有人能建议我在 firebase 中解决这种类型的冲突,我会非常高兴(可能是通过定义一些规则和一些额外的逻辑)。
【问题讨论】:
-
我将建议您为 JSON 对象添加时间戳,并使用安全规则确保写入的数据是最新的。但后来我在Firebase Reference 上看到
ServerValue.TIMESTAMP包含“由 Firebase 服务器确定的当前时间戳”。现在我想知道当client2推送到数据库的数据被写入时会显示什么时间。我会等待其他人回答。
标签: firebase firebase-realtime-database