Firestore unsubscribe to updates

前端 未结 1 1886
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 07:19

I am following this Google Cloud Firestore example on YouTube and getting real time updates successfully. However, I don\'t know how to unsubscribe to updates because it\'s

相关标签:
1条回答
  • 2021-01-12 07:46

    The firestore.collection().onSnapshot() function returns a unsubscribe function. Just call it and you should be guchi.

    You can also find another example here: How to remove listener for DocumentSnapshot events (Google Cloud FireStore)

    Here is a snippet I created that should work:

    let unsubscribe;
    
    getRealtimeUpdates = function(document) {
    		unsubscribe = firestore.collection("collection_name")
    			.onSnapshot(function(querySnapshot) {
    			querySnapshot.forEach(function(doc) {
    				if (doc && doc.exists) {
    					const myData = doc.data();
    					// DO SOMETHING
    				}
    			});
    		});
    	}
      
      // unsubscribe:
      
      unsubscribe();

    Path to the corresponding Firebase Documentation:

    https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#onSnapshot

    Returns An unsubscribe function that can be called to cancel the snapshot listener.

    0 讨论(0)
提交回复
热议问题