简体   繁体   English

firestore 取消订阅完成事件

[英]firestore unsubscribe finished event

I am subscribed to a document in firestore, before I logout I want to unsubscribe otherwise it will throw an error since I am using firestore security rules and when the user is logged out and I didn't unsubscribe the access will be denied and it will throw an error.我订阅了firestore中的一个文档,在我注销之前我想取消订阅,否则它会抛出一个错误,因为我正在使用firestore安全规则并且当用户注销并且我没有取消订阅时,访问将被拒绝并且它会抛出错误。

I tried the code below but still not working I still get errors from firestore security rules, so I introduced setInterval but it is taking too much time (in my test I have gone till 5s to be able to avoid the error)我尝试了下面的代码但仍然无法正常工作我仍然从firestore安全规则中得到错误,所以我引入了setInterval但它花费了太多时间(在我的测试中我一直到5秒才能避免错误)

Do you have a better approach to avoid this issue, I mean is there an event that is fired once the unsubscription is done (I want to avoid setInterval with long time)?你有没有更好的方法来避免这个问题,我的意思是一旦取消订阅就会触发一个事件(我想长时间避免 setInterval)?

var unsubscribe = db.collection('notifications').doc('docID').onSnapshot(
          function(doc) {
            // Do things
          })

function logout () {
  unsubscribe(
    setTimeout(function() {
      firebase.auth().signOut().then(() => {
        // Redirect to home page
      });
    }, 600)
  )
};

As far as I know, unsubscribing is synchronous.据我所知,退订是同步的。 So when the unsubscribe() is done, you can just log out:所以当unsubscribe()完成后,你可以退出:

unsubscribe();
firebase.auth().signOut().then(() => {
    // Redirect to home page
});

If you're still getting security errors after this, you might still have another listener attached for the user.如果在此之后您仍然遇到安全错误,您可能仍然为用户附加了另一个侦听器。 If that doesn't seem to be the case, please edit your question to include the minimal, complete/standalone code that we can run to reproduce the problem .如果情况似乎并非如此,请编辑您的问题以包含我们可以运行以重现问题的最小、完整/独立代码 Alternatively, you can (also) set up a minimal reproduction of the problem on a site like jsbin.或者,您可以(也)在 jsbin 之类的网站上设置问题的最小再现。

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

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