I'm new in Google Cloud FireStore.
The Document object has a function call onSnapshot to attaches a listener for DocumentSnapshot events.
Is there a function to remove that listener (like offSnapshot)? If not how can I implement it?
You'll need to update MainActivity. this to match your code. By passing in the activity, Firestore can clean up the listeners automatically when the activity is stopped. Yet another alternative is to use get() to get those nested documented, which just reads the document once.
Set the event handler Functions let you handle Realtime Database events at two levels of specificity; you can listen for specifically for only creation, update, or deletion events, or you can listen for any change of any kind to a path.
A DocumentSnapshot contains data read from a document in your Cloud Firestore database. The data can be extracted with the getData() or get(String) methods. If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null .
In case of the web and node.js SDK, calling onSnapshot
returns a function that you need to save in a variable and call when you want to remove the listener.
var unsubscribe = db.collection("cities").onSnapshot(function (querySnaphot) { // do something with the data. }); // Stop listening to changes unsubscribe();
The other SDKs offer similar functionality.
See https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With