有关以下代码示例的快速问题:
firebase.firestore()
.collection("chatrooms")
.doc(`${chatId}`)
.collection(`${chatId}`)
.orderBy("timestamp")
.limit(50).onSnapshot((snapshot) => {
//doing something with snapshot.val()
})我已经设置了那个侦听器到我的消防站,它总是只为我检索50个最新的文档。我的问题是:,如果我想分离这个听者,我能这样做吗?
firebase.firestore().collection("chatrooms").doc(`${chatId}`).collection(`${chatId}`).off();我问这个问题是因为我不确定是否只在引用上调用.off() --在这种情况下有效。我知道它通常是这样做的,但我的询问是有序的和有限的,所以这就是我问的原因。
发布于 2021-08-27 12:09:04
在off()上没有任何CollectionReference方法。onSnapshot方法返回一个函数,该函数可以用于删除侦听器,而不管查询是什么。
const unsubscribe = firebase.firestore()
.collection("chatrooms")
.doc(`${chatId}`)
.collection(`${chatId}`)
.orderBy("timestamp")
.limit(50).onSnapshot((snapshot) => {
//doing something with snapshot.val()
})
// run this to remove listener
unsubscribe()如果您需要从任何其他组件中取消订阅侦听器,则可能必须使用支持传递此函数。
https://stackoverflow.com/questions/68953076
复制相似问题