最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase .get() vs .once() - What is the difference? - Stack Overflow

programmeradmin3浏览0评论

Documentation here but I don't understand: What is the difference between .get() and .once()?

My understanding is that .get() reads from the server, and then from the local storage, and .once() reads only from the local storage. I have used.once() to retrieve values without setting up a listener like .on() and it seems to be retrieving values from the server (realtime database) that I haven't retrieved before so I'm not sure what it means when it says it only searches the local storage... It seems like it is in fact getting values from the server.

Please clarify when to use one vs the other.

I have not used .get() at all.

Documentation here but I don't understand: What is the difference between .get() and .once()?

My understanding is that .get() reads from the server, and then from the local storage, and .once() reads only from the local storage. I have used.once() to retrieve values without setting up a listener like .on() and it seems to be retrieving values from the server (realtime database) that I haven't retrieved before so I'm not sure what it means when it says it only searches the local storage... It seems like it is in fact getting values from the server.

Please clarify when to use one vs the other.

I have not used .get() at all.

Share Improve this question asked Mar 13, 2021 at 0:03 Jeremy IronsJeremy Irons 5346 silver badges17 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

firebaser here

In practice, in the web SDK, there is no practical difference between get() and once('value', since the web SDK for Realtime Database. does not support disk persistence.

We mostly decided to add the get() method to the JavaScript SDK, as we were adding similar methods to the iOS and Android SDKs, where there is a real, but subtle, difference between the methods. For a longer explanation of that, see What is the difference between get() and addListenerForSingleValueEvent? (addListenerForSingleValueEvent is the Android variant of JavaScript's once() call).

.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.

I hope this clarifies.

There is no technical difference per Frank's answer below, but in practice, I receive a ton of "Client is offline" Errors for seemingly no reason when using .get() in Firebase Functions.

I recommend using .once()

See issue here for reference: https://groups.google.com/g/firebase-talk/c/VpQms_TnBOw?pli=1

发布评论

评论列表(0)

  1. 暂无评论