ref路径不再可用时如何删除Firebase数据库侦听器 [英] How to remove firebase database listener when ref path is no longer available

查看:67
本文介绍了ref路径不再可用时如何删除Firebase数据库侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在React Native应用中使用Firebase,并且我有一个类似于以下的监听器:

I'm using firebase in a react native app and I have a listener similar to this:

firebase.database().ref('users/'+uid+'/items').on('value', function(snapshot){
    ....
})

我要执行的操作是在用户注销时删除该侦听器.我知道我可以像这样使用off()来做到这一点:

What I'm trying to do is to remove that listener when the user logs out. I know that I can do it using off() like:

firebase.database().ref('users/'+uid+'/items').off('value', function(snapshot){
    ....
})

但是,一旦用户注销,我将无法再访问该用户ID.那么如何删除监听器?

But once the user logs out I no longer have access to the user id. So how do I remove the listener?

推荐答案

您需要保留连接侦听器的路径,以便可以在其上调用off().一种方法是:

You'll need to retain the path on which you've attached the listener so that you can call off() on it. One way to do this would be:

mUserRef = firebase.database().ref('users/'+uid+'/items');
mUserRef.on('value', function(snapshot){
    ....
})

再后来:

mUserRef.off('value');

这篇关于ref路径不再可用时如何删除Firebase数据库侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆