As the title suggests I'm just wondering what would a good/best practice be for refreshing the data on the client side after adding a new document to the firestore. So basically to summerize the functionality, let's say I have a form and below the form are all the documents. What would be a good way to refresh the data so I don't stress out firebase. Adding new documents is a good chunk of the app I'm working on, so it'll probably be used a lot. I'm wondering should I just add it client side if the doc was added successfully or should I just send a call for all the docs again or something else. I'm familiar with the snapshot option, I'm just not sure if it's a good practice. Thank you in advanced.
CodePudding user response:
The ideal solution would be to use snapshots. Let's look at an abstract from their documentation.
Local writes in your app will invoke snapshot listeners immediately. This is because of an important feature called
latency compensation
. When you perform a write, your listeners will be notified with the new data before the data is sent to the backend.Retrieved documents have a
metadata.hasPendingWrites
property that indicates whether the document has local changes that haven't been written to the backend yet. You can use this property to determine the source of events received by your snapshot listener.
So you see, you adding the documents to the client side directly is the same as using a firestore snapshot
because in case of local writes, the data is directly received by the listeners and will not go to the server first and then receive the data.
Also, you could even write a query to limit
& sort
the data and the snapshot listener will automatically handle the logic for limiting and sorting the data. Nevertheless, you can even query the data once the document is added on the client side using get()
, but that's just beating around the bushes.
Snapshots
were designed for this purpose of bi-directional
updates and I would highly recommend you to use them instead.