Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove listener for DocumentSnapshot events (Google Cloud FireStore)

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?

like image 610
Minh Nguyen Avatar asked Oct 09 '17 09:10

Minh Nguyen


People also ask

How do I remove firestore listener from my Android?

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.

Which function is used for users to listen monitor for changes in the firebase?

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.

What is DocumentSnapshot firestore?

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 .


1 Answers

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.

like image 89
Scarygami Avatar answered Sep 22 '22 03:09

Scarygami