Firestore取消订阅更新 [英] Firestore unsubscribe to updates

查看:29
本文介绍了Firestore取消订阅更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在YouTube上遵循此 Google Cloud Firestore 示例并获得实时更新成功.但是,我不知道如何退订更新,因为视频中没有对此进行解释.我阅读了文档以创建unsubscribe()函数,但对我而言不起作用.

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 not explained in the video. I read the documentation to create an unsubscribe() function but it doesn't work for me.

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

推荐答案

firestore.collection().onSnapshot()函数返回一个取消订阅的函数.只是称呼它,你应该是guchi.

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

您还可以在此处找到另一个示例:如何删除DocumentSnapshot事件的侦听器(Google Cloud FireStore)

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();

相应的Firebase文档的路径:

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.

这篇关于Firestore取消订阅更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆