问题描述
有什么办法可以在不删除Firestore监听器的情况下将其暂停?
Is there any way to pause firestore listener without removing it?
我有多个Firebase侦听器,其中一些依赖于其他侦听器,这些侦听器会更改或在数据更改时启动其他侦听器.假设我的第一个侦听器在 onSnapshot
上启动了第二个侦听器.第一个监听器始于 useEffect
.在某些情况下,我可能不想更改第二个侦听器,因此我需要丢弃第一个侦听器的数据更改更新.
I have multiple firebase listeners, some are dependent on other, that changes or start other listeners on data change. Lets say my first listener starts a second listener its onSnapshot
. First listener started on useEffect
. For certain condition I may not want to change the second listener, so I need to discard data change update from first listener.
如果满足条件(单击按钮),我会暂时放弃第一个侦听器上的数据更改.目前,我正在使用带有 useRef
的布尔值.我的react应用可以正常运行,并且具有这样的依赖侦听器.我可以删除该侦听器,但是我不想删除并重新创建该侦听器.
If condition met (button click), I discard data changes on first listener for a few moments. Currently I'm doing this using a boolean with useRef
. My react app is working fine, with dependant listeners like this. I could remove the listener but I do not want to remove and recreate the listener.
我想知道是否有暂停机制或方法可用于任何侦听器.我认为,如果有这样的方法,将节省很少的读取成本,因为我没有使用在Snapshot上发送的数据.
I was wondering if there is a pausing mechanism or method available for any listener. I think it will save a tiny read cost if there was such a method because I'm not using that data sent onSnapshot.
代码示例:
useEffect(() => {
let firstListener, secondListener;
//console.log("useEffect...");
function ListenerFunc(p) {
secondListener = await firestore
.collection("test")
.doc(p)
.onSnapshot((doc) => {
//console.log("Current data: ", doc.data());
//Need to discard unwanted change here.
//Changing it on button click for a 2 seconds then it changes back to : pauser.current = false.
if (pauser.current) {
console.log("paused for a moment.");
//pauser.current = false;
return;
}
else {
//update.
}
})
}
firstListener = firestore
.collection("test")
.doc("tab")
.onSnapshot((doc) => {
//console.log("Current data: ", doc.data());
var p = doc.data().p; //get variable p
ListenerFunc(p);
});
// cleanup.
}
推荐答案
不幸的是,这是不可能的.如果您需要停止监听更改,即使是暂时的,则必须断开监听器并在要再次开始监听时附加一个新的监听器,监听器没有暂停机制.
Unfortunately this is not possible. If you need to stop listening for changes, even temporarily, you have to detach your listener and attach a new one when you want to start listening again, there is no pause mechanism for listeners.
您可以在 Google的问题跟踪器如果您希望产品团队可以考虑这一点,但是鉴于此针对IOS SDK的GitHub功能请求,但该请求被拒绝了,我认为这不会很快改变.
You could open a Feature Request in Google's Issue Tracker if you'd like so that the product team can consider this, but given that this has already been proposed in this GitHub Feature Request for the IOS SDK and it was rejected I don't see this changing anytime soon.
这篇关于有什么办法可以在不删除Firestore监听器的情况下将其暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!