Firebase客户端如何知道要同步哪些数据? [英] How does firebase client know what data to sync?

查看:102
本文介绍了Firebase客户端如何知道要同步哪些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络丢失并重新建立连接的情况下,任何人都可以分享一些有关Firebase移动客户端如何确切知道要从服务器下载哪些数据的知识吗?

Could anyone share some knowledge on how exactly firebase mobile client knows what data to download from the server in case network connection is lost and re-established?

我们这样说:

  • 数据库中存在一个键为key1和key3的对象.
  • 移动客户端下载了它们并使其离线.
  • 与此同时,添加了key2和key4
  • 客户端恢复连接并以某种方式接收key2和key4.

它究竟如何知道发生了什么变化?

How exactly does it know what has changed?

  • 它会再次下载整个收藏集吗?
  • 是否以某种方式交换所有对象的键以比较丢失的对象?
  • 服务器是否记住哪些客户端有什么?
  • 还有什么?

推荐答案

服务器会跟踪每个客户端的活动侦听器,但不会跟踪每个客户端知道的信息.

The server keeps track of the active listeners for each client, but it does not track what each client knows.

当您将侦听器附加到本地缓存中的位置时,客户端将计算缓存数据所有分支的组合哈希键.它将这些哈希发送到服务器,服务器对实际数据执行相同的哈希处理.如果任何哈希值不同,则说明数据已被修改,因此它已从服务器发送回客户端.

Instead when you attach a listener to a location that is in your local cache, the client calculates the composite hash keys of all branches of the cache data. It sends those hashes to the server, which performs the same hashing on the actual data. If any of the hashes is different, then data was modified and so it it sent back from the server to the client.

请注意,此哈希/交换仅发生在已缓存并附加侦听器的数据上.您没有在听的任何缓存数据都不会得到这种处理.由于通常会在应用程序的整个生命周期中将侦听器添加到数据的不同部分,因此散列和同步的成本会在应用程序的整个生命周期内分散.我仅有的一次仁慈的举动是开发人员在数据库的根目录上调用了keepSynced(true),这实际上在应用程序启动时强制进行巨型同步".在这种情况下,本地缓存的散列及其同步可能会花费很短的时间,内存和带宽.

Note that this hashing/exchange only happens for data that you have cached and attach a listener to. Any cached data that you are not listening to does not get this treatment. Since you typically add listeners to the different parts of your data over the lifetime of your app, this cost of the hashing and synchronizing spreads out over the lifetime of the app. The only times where I have seen a less benevolent behavior is where developers had called keepSynced(true) on the root of their database, which essentially forces a "giant sync" when your app starts. In that scenario the hashing of the local cache and its synchronization may take non-trivial time, memory and bandwidth.

这篇关于Firebase客户端如何知道要同步哪些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆