在Firebase中启用持续性后会发生什么? [英] What actually happens when persistence is enabled in Firebase?

查看:105
本文介绍了在Firebase中启用持续性后会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个好主意,它应该如何工作

在iOS中开启Firebase持久性时,我的观察者实际上发生了什么事情?基于这个 https://www.firebase.com /docs/ios/guide/offline-capabilities.html - Firebase本质上保留了您可以在离线状态下访问的数据的缓存副本。



不明白的是我的观察者应该触发多少次以及提供什么信息。


  • firebase是否总是触发我的观察者,一次使用任何缓存数据(如果没有任何数据,则返回null),然后是服务器数据。
  • 或者它只发送缓存的数据(如果存在的话),然后是服务器数据。 b $ b
  • 在持久性模式下,observerSingleValue和持续观察者的行为是否有区别?



在启用持续性的应用程序中,我注意到:


  • Firebase只是发送服务器数据

  • Firebase发送缓存的数据(如果存在的话),然后发送服务器数据。
  • Firebase发送缓存的数据,如果不存在,则后跟服务器数据。



最好把这个清理出来,这样我们就知道哪个应该是正常的行为了:

解决方案

其实很简单。当您附加观察者(无论使用 observeEventType 还是 observeSingleEventOfType )时,Firebase将会:


  1. 使用任何完整的缓存数据立即引发事件。
  2. 从服务器请求更新的数据,它会到达,如果数据与缓存的数据不同,则引发新的事件。

    $ b $

    有一些微妙之处:




    • 如果缓存数据完整,我们只会引发事件。这意味着:


      • 如果我们没有缓存的数据(您以前没有观察过此位置),我们将不会 与null或类似。在从服务器获取数据之前,您将不会收到任何事件。

      • 如果您有此部分的部分数据(例如,您以前观察过/ foo / bar但现在你正在观察/ foo),你会得到 ChildAdded 事件为完整的孩子(例如/ foo / bar),但是你不会得到事件(例如/ foo),直到我们从服务器获取完整的数据,观察您所在的位置。
      • >
      • 如果您使用的是 observeSingleEventOfType ,那么您明确要求只有一个事件,所以如果您有缓存的数据,则会发生#1但#2不会,这可能不是你想要的(你永远不会看到最新的服务器数据)。


        帮助!


        When turning on persistence for Firebase in iOS what actually happens to my observers and how they behave on a technical level?

        I have a good idea how it should work on a high-level based on this https://www.firebase.com/docs/ios/guide/offline-capabilities.html - Firebase essentially keeps a cached copy of the data you can access whilst offline.

        What I don't understand is how many times my observers should fire and with what information.

        • Does firebase always trigger my observers once with any cached data first (or null if there isn't any data) followed by the server data.
        • Or does it only send the cached data if it exists followed by the server data.
        • Is there any difference between observerSingleValue and a continous observer's behaviour when in persistence mode ?

        In our app with persistence enabled, I have noticed:

        • Firebase just sending the server data
        • Firebase sending the cached data if it exists then the server data.
        • Firebase sending the cached data and null if it doesn't exist followed by the server data.

        It would be good to clear this up so we know which should be the normal behaviour :)

        解决方案

        It's actually pretty simple. When you attach an observer (whether using observeEventType or observeSingleEventOfType), Firebase will:

        1. Immediately raise events with any complete cached data.
        2. Request updated data from the server and, when it arrives, raise new events if the data is different than what was cached.

        There are a couple subtleties that fall out of this though:

        • We'll only raise events with cached data if it is complete. This means:
          • If we have no cached data (you haven't observed this location before), we will not raise events with null or similar. You won't get any events until we get data from the server.
          • If you have partial data for this location (e.g. you observed /foo/bar previously but now you're observing /foo), you will get ChildAdded events for complete children (e.g. /foo/bar), but you won't get a Value event (e.g. for /foo) until we've gotten complete data from the server for the location you're observing.
        • If you're using observeSingleEventOfType, you're explicitly asking for only a single event and so if you have cached data, #1 will happen but #2 will not, which may not be what you want (you'll never see the latest server data).

        Hope this helps!

        这篇关于在Firebase中启用持续性后会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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