简体   繁体   English

有什么方法可以暂停firestore监听器而不删除它?

[英]Any way to pause firestore listener without removing it?

Is there any way to pause firestore listener without removing it?有什么方法可以暂停firestore监听器而不删除它?

I have multiple firebase listeners, some are dependent on other, that changes or start other listeners on data change.我有多个 firebase 侦听器,有些依赖于其他侦听器,这些侦听器会在数据更改时更改或启动其他侦听器。 Lets say my first listener starts a second listener its onSnapshot .假设我的第一个监听器启动了第二个监听onSnapshot First listener started on useEffect .第一个监听器从useEffect开始。 For certain condition I may not want to change the second listener, so I need to discard data change update from first listener.在某些情况下,我可能不想更改第二个侦听器,因此我需要丢弃来自第一个侦听器的数据更改更新。

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 .目前我正在使用带有 useRef 的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.我可以删除监听器,但我不想删除并重新创建监听器。

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.我认为如果有这样的方法会节省一点读取成本,因为我没有使用在快照上发送的数据。

Code example:代码示例:

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.如果您需要停止监听更改,即使是暂时的,当您想再次开始监听时,您必须分离监听器并附加一个新监听器,监听器没有暂停机制。

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.如果您愿意,您可以在Google 的问题跟踪器中打开功能请求,以便产品团队可以考虑这一点,但鉴于此GitHub IOS ZF20E3C5E54C0AB3D375AZD660B3F 的功能请求中已经提出了这一点,并且它被拒绝了。这种情况很快就会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM