Firebase .get() 与 .once() - 有什么区别?

Jer*_*ons 8 javascript firebase firebase-realtime-database

这里有文档,但我不明白:What is the Difference Between .get() and .once()?

我的理解是.get()从服务器读取,然后从本地存储读取,并且.once()只从本地存储读取。我曾经.once()在不设置侦听器的情况下检索值.on(),它似乎是从服务器(实时数据库)检索我之前没有检索过的值,所以我不确定当它说它只搜索本地时这意味着什么存储...看起来它实际上是从服务器获取值。

请说明何时使用其中一种或另一种。

.get()我根本没用过。

Fra*_*len 11

火力战士在这里

get()实际上,在 Web SDK 中,和之间没有实际区别once('value',因为 Web SDK 用于实时数据库。不支持磁盘持久化。

我们主要决定将该方法添加get()到 JavaScript SDK,因为我们向 iOS 和 Android SDK 添加了类似的方法,这些方法之间存在真正但微妙的差异。有关详细说明,请参阅get() 和 addListenerForSingleValueEvent 之间有什么区别?addListenerForSingleValueEvent是 JavaScript 调用的 Android 变体once())。

  • 我与工程师一起定义了这个 API,所以我非常清楚这个 API 如何在所有平台上工作。JavaScript SDK 没有磁盘缓存,因此获得该值的唯一方法是将其保存在内存中,这意味着您需要有一个活动的“on()”侦听器,这也解释了为什么“ Once()` 和 `get()` 将返回内存中的值。如果他们中的任何一个做了不同的事情或者在这种情况下从网络检索值,那么这就是实现中的错误。例如:Android 实现中曾短暂存在过这样的 bug,但已被修复。 (4认同)

Nom*_*dme 8

.get() will always try to make a call to the database, IF only when if it is not able to reach the database or similar, it will fall back to the local cache.

on the other hand; .once() will always try to get the value from the local cache, instead of making the call to the database, making it cost effective.

You can also look at the reference on the method which makes it more clear:

.get() 
Gets the most up-to-date result for this query.

Returns Promise<DataSnapshot>
A promise which resolves to the resulting DataSnapshot if a value is available, 
or rejects if the client is unable to return a value 
(e.g., if the server is unreachable and there is nothing cached).

.once()
Listens for exactly one event of the specified event type, 
and then stops listening.

This is equivalent to calling on(), and then calling off() inside 
the callback function.
Run Code Online (Sandbox Code Playgroud)

I hope this clarifies.


Jer*_*ons 3

下面Frank的回答没有技术差异,但实际上,在 Firebase Functions 中使用时,我似乎毫无理由地收到大量“客户端离线”错误.get()

我建议使用.once()

请参阅此处的问题以供参考:https://groups.google.com/g/firebase-talk/c/VpQms_TnBOw?pli =1