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

rubotero

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?

Renaud Tarnec

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Firebase/cloud firestore: onSnapshot() vs on()

From Java

What's the difference between Cloud Firestore and the Firebase Realtime Database?

From Dev

Get Difference Between Dates

From Java

How does one get an async React function calling firebase.firestore.set to wait whilst using an onSnapshot callback?

From Dev

Cloud Firestore query to get doc ID, Flutter

From Dev

Get/add Document in Cloud functions using Firestore

From Java

What is the difference between Cloud Foundry and Docker?

From Java

Firebase Cloud Functions: Difference between onRequest and onCall

From Dev

What is the difference between Cloud Foundry and OpenWhisk?

From Java

What is the difference between Cloud Functions and Firebase Functions?

From Dev

Difference between Cloud Foundry & Pivotal Web Services

From Dev

What is the difference between Solr Replication and Solr Cloud?

From Dev

What is the difference between Depth Data and Point Cloud?

From Dev

What is the difference between Solr Replication and Solr Cloud?

From Dev

get difference between 3 lists

From Dev

Difference between GET and PROPFIND in WebDAV

From Java

Get difference between two lists

From Dev

Is there a difference between .get() and -> with smart pointers?

From Dev

Get the difference between two strings

From Dev

Get the difference between two list?

From Dev

difference between get('doctrine'); and getDoctrine();

From Dev

Difference between get and match in rails

From Dev

Groovy - difference between get and propertyMissing?

From Dev

Difference between get and dunder getitem

From Dev

What is the difference between get() and addListenerForSingleValueEvent?

From Dev

Get difference between to dates php

From Dev

Is there a difference between .get() and -> with smart pointers?

From Dev

Difference between get and resource with except

From Dev

What is the difference between data() and get()

Related Related

  1. 1

    Firebase/cloud firestore: onSnapshot() vs on()

  2. 2

    What's the difference between Cloud Firestore and the Firebase Realtime Database?

  3. 3

    Get Difference Between Dates

  4. 4

    How does one get an async React function calling firebase.firestore.set to wait whilst using an onSnapshot callback?

  5. 5

    Cloud Firestore query to get doc ID, Flutter

  6. 6

    Get/add Document in Cloud functions using Firestore

  7. 7

    What is the difference between Cloud Foundry and Docker?

  8. 8

    Firebase Cloud Functions: Difference between onRequest and onCall

  9. 9

    What is the difference between Cloud Foundry and OpenWhisk?

  10. 10

    What is the difference between Cloud Functions and Firebase Functions?

  11. 11

    Difference between Cloud Foundry & Pivotal Web Services

  12. 12

    What is the difference between Solr Replication and Solr Cloud?

  13. 13

    What is the difference between Depth Data and Point Cloud?

  14. 14

    What is the difference between Solr Replication and Solr Cloud?

  15. 15

    get difference between 3 lists

  16. 16

    Difference between GET and PROPFIND in WebDAV

  17. 17

    Get difference between two lists

  18. 18

    Is there a difference between .get() and -> with smart pointers?

  19. 19

    Get the difference between two strings

  20. 20

    Get the difference between two list?

  21. 21

    difference between get('doctrine'); and getDoctrine();

  22. 22

    Difference between get and match in rails

  23. 23

    Groovy - difference between get and propertyMissing?

  24. 24

    Difference between get and dunder getitem

  25. 25

    What is the difference between get() and addListenerForSingleValueEvent?

  26. 26

    Get difference between to dates php

  27. 27

    Is there a difference between .get() and -> with smart pointers?

  28. 28

    Difference between get and resource with except

  29. 29

    What is the difference between data() and get()

HotTag

Archive