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

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

问题描述

在 iOS 中为 Firebase 启用持久性时,我的观察者实际发生了什么以及他们在技术层面上的表现如何?

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

我很清楚它应该如何基于此https://firebase.google.com/docs/database/ios/offline-capabilities - Firebase 本质上保留了您可以在离线时访问的数据的缓存副本.

I have a good idea how it should work on a high-level based on this https://firebase.google.com/docs/database/ios/offline-capabilities - 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.

  • firebase 是否总是首先使用任何缓存数据(如果没有任何数据则为 null)然后是服务器数据触发我的观察者一次.
  • 或者它是否只发送缓存数据(如果存在),然后是服务器数据.
  • 在持久模式下,observerSingleValue 和连续观察者的行为有什么区别吗?

在我们启用持久性的应用中,我注意到:

In our app with persistence enabled, I have noticed:

  • Firebase 只是发送服务器数据
  • Firebase 发送缓存数据(如果存在),然后发送服务器数据.
  • Firebase 发送缓存数据,如果不存在则发送 null,然后发送服务器数据.

最好解决这个问题,这样我们就知道哪些应该是正常行为:)

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

推荐答案

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

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

  1. 使用任何完整缓存数据立即引发事件.
  2. 从服务器请求更新的数据,当数据到达时,如果数据与缓存的数据不同,则引发新事件.
  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:

  • 我们只会在缓存数据完成时引发事件.这意味着:
    • 如果我们没有缓存数据(您之前没有观察过这个位置),我们将不会引发 null 或类似事件.在我们从服务器获取数据之前,您不会收到任何事件.
    • 如果您有此位置的部分数据(例如,您之前观察了/foo/bar,但现在观察/foo),您将获得 ChildAdded 事件完整的孩子(例如/foo/bar),但是在我们从服务器获取您正在观察的位置的完整数据之前,您不会收到 Value 事件(例如/foo).
    • 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.

      希望这有帮助!

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

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