Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between get() and onSnapshot() in Cloud Firestore

I am reading some data from Firebase's Cloud Firestore but I've seen several ways to do it. The example I saw used the get and onSnapshot function like this:

db.collection("cities").doc("SF")  .onSnapshot(doc => {       console.log(doc.data());  }); 

or this

var docRef = db.collection("cities").doc("SF");  docRef.get().then(doc => {     if (doc.exists) {          console.log("Document data:", doc.data());     } else {          console.log("No such document!");     } }).catch(function(error) {    console.log("Error getting document:", error);         }); 

Is there any difference between them?

like image 381
rubotero Avatar asked Feb 01 '19 12:02

rubotero


People also ask

What is firestore onSnapshot?

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

How do I get data from firestore snapshot?

There are three ways to retrieve data stored in Cloud Firestore. Any of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data once. Set a listener to receive data-change events.

What does firebase firestore () do?

Cloud Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps - at global scale.

How do I get data from QueryDocumentSnapshot?

A QueryDocumentSnapshot contains data read from a document in your Cloud Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted using the getData() or the various get() methods in DocumentSnapshot (such as get(String) ).


1 Answers

As explained in the doc:

There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:

  • Call a method to get the data.
  • Set a listener to receive data-change events.

When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes.

When you use get() you "retrieve the content of a single document" only once. It's a kind of "get and forget": If the document changes in the (back-end) Firestore database you will need to call get() again to see the change.

On the opposite, if you use the onSnapshot() method you constantly listen to a document as explained in the doc:

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

As explained in these docs, these two methods apply to one document or to a collection of documents (including a query).

like image 123
Renaud Tarnec Avatar answered Sep 30 '22 15:09

Renaud Tarnec