Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 916
Jeremy Irons Avatar asked Mar 13 '21 00:03

Jeremy Irons


People also ask

What is the difference between real time database and firestore?

Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.

How do I get data from firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

What is DataSnapshot in firebase?

firebase. database. DataSnapshot. A DataSnapshot contains data from a Database location. Any time you read data from the Database, you receive the data as a DataSnapshot .

What is uf8ff firebase?

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText .

What is the difference between Firebase and parse?

Firebase provides central data management, whereas the Parse server uses different databases to store different kinds of databases. For instance, MongoDB for data storage and Amazon s3 bucket for file storage.

Does firebase support realtime data syncing?

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing: Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model.

What is the difference between Firebase and Google Cloud?

Google Cloud is a more comprehensive platform and delivers more services than Firebase. The list of services includes computing engines, storage, CDN, Kubernetes, BigQueries, etc. It’s also important to emphasize that Firebase is part of Google Cloud, and the acquisition happened in 2014.

What are the advantages of using Firebase for app development?

It offers cloud storage & also helps developers to construct their apps quicker and in a more secure way. No programming is required on the firebase side which makes it convenient to use its elements more efficiently. It makes use of NoSQL for the database for the storage of data.


2 Answers

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

like image 71
Frank van Puffelen Avatar answered Oct 08 '22 03:10

Frank van Puffelen


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

like image 7
Ganbayar Gansukh Avatar answered Oct 08 '22 03:10

Ganbayar Gansukh