简体   繁体   中英

Any way to pause firestore listener without removing it?

Is there any way to pause firestore listener without removing it?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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