Home > Software engineering >  Firestore: are documents that are part of a onSnapshot query pre-loaded?
Firestore: are documents that are part of a onSnapshot query pre-loaded?

Time:06-28

I want to make sure I'm understanding Firestore fetch semantics properly.

I have a query that gets a list of documents from Firestore, from a collection called "example"

onSnapshot(myQuery of example collection, snapshot => {
   // contains various documents
   // including documentId = 123abc
})

In a different part of my application, I set up onSnapshot for a single document (that was included in the results of the previous query):

onSnapshot(doc(db, "example", "123abc"), snapshot => [...])

So the document that I'm setting up a listener for already has a listener, as part of the overall query listener used in a different part of the app (but still running).

My question is: Does adding a snapshot to the individual document result in another read from Firestore, or does it automagically use the same document listener and not add another read? If so, does that hold for changes to the document?

Let me know if you need me to clarify the question at all. Thanks in advance!

CodePudding user response:

The Firebase SDK deduplicates listeners behind the scenes, so adding an additional snapshot listener for a document that is already in memory will give you that snapshot from memory and will not reread the document from the server.

  • Related