Firestore 取消订阅更新 [英] Firestore unsubscribe to updates
问题描述
我正在 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/参考/js/firebase.firestore.CollectionReference#onSnapshot
退货一个取消订阅函数,可以调用它来取消快照监听器.
Returns An unsubscribe function that can be called to cancel the snapshot listener.
这篇关于Firestore 取消订阅更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!