Firebase值事件侦听器无法正常工作 [英] Firebase value event listener not working

查看:125
本文介绍了Firebase值事件侦听器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个应该从Firebase实时数据库中删除的ID。如果我把它拉下来,它看到没有可用的id,那么它将id设置为1.我做了一个调试,id设置为1,但是在侦听器结束之后,id变为0。不知道为什么或是什么原因造成这种情况,但这里是我的代码。



监听者代码:

  userRef.child( ID)。 
$ b $ addSingleValueEventListener(new ValueEventListener(){
@Override public void onDataChange(DataSnapshot dataSnapshot){
try {
String value = dataSnapshot.getValue()。toString( );
id = Integer.parseInt(value);
} catch(NullPointerException e){
id = 1; //在此之后,id是1
e.printStackTrace );
}
}

@Override public void onCancelled(DatabaseError databaseError){
id = 1;
}
}); //现在id是0


解决方案

从这个问题,一个关于Firebase的听众,第二个关于删除多余的代码。

关于Firebase侦听器的一个基本的事情是它们是异步,这意味着您的代码在执行下一行之前不会等待结果。所以看看这个框架代码中的注释:

  userRef.child(id)。 
addSingleValueEventListener(new ValueEventListener(){
@Override public void onDataChange(DataSnapshot dataSnapshot){
//此处的代码不会立即执行,
//只要执行从远程数据库接收数据
}

@Override public void onCancelled(DatabaseError databaseError){


});
//如果在添加监听器
之后立即有一行代码// onDataChange代码*将不会运行*。你直接去这些
//代码行,onDataChange方法会在它准备好的时候运行。

所以这就意味着如果你想用<$ c $你应该把代码放在 onDataChange 方法中(或者从那里调用的其他方法,或者以其他方式运行代码 > 数据已被传回)



关于第二部分,稍微多一些检查存在的Firebasey 方法的一个int并获取值将是:

$ @ $> $ $ $ $ $ @Override public void onDataChange(DataSnapshot dataSnapshot){
if( dataSnapshot.exists()){
id = dataSnapshot.getValue(Integer.class);
} else {
id = 1;
}
}


In my app, I have an id that is supposed to be pulled down from firebase real time database. If I pull it down and it sees there is not an id available, then it sets the id to 1. I did a debug, and the id is set to 1, but after the listener finishes, the id goes to 0. I don't know why or what is causing this, but here is my code.

Code for listener:

userRef.child("id").

addSingleValueEventListener(new ValueEventListener() {
    @Override public void onDataChange (DataSnapshot dataSnapshot){
        try {
            String value = dataSnapshot.getValue().toString();
            id = Integer.parseInt(value);
        } catch (NullPointerException e) {
            id = 1;  //After this, id is 1
            e.printStackTrace();
        }
    }

    @Override public void onCancelled (DatabaseError databaseError){
        id = 1;
    }
}); //Now id is 0

解决方案

Two things come to mind from this question, one about Firebase listeners and the second about removing excess code.

A fundamental thing about Firebase listeners is that they are asynchronous, meaning your code does not wait for the result before executing the next line. So look at the comments in this skeleton code:

userRef.child("id").
addSingleValueEventListener(new ValueEventListener() {
    @Override public void onDataChange (DataSnapshot dataSnapshot){
        // code here does not get executed straight away,
        // it gets executed whenever data is received back from the remote database
    }

    @Override public void onCancelled (DatabaseError databaseError){

    }
});
// if you have a line of code here immediately after adding the listener
// the onDataChange code *won't have run*. You go straight to these lines
// of code, the onDataChange method will run whenever it's ready.

So this means that if you want to do something with the data you are getting in onDataChange you should put that code inside the onDataChange method (or some other method called from there or in some other way running that code after the data has been delivered back).

Regarding the second part, a slightly more Firebasey way of checking for existence of an int and getting the value would be:

@Override public void onDataChange (DataSnapshot dataSnapshot){
    if (dataSnapshot.exists()) {
        id = dataSnapshot.getValue(Integer.class);
    } else {
        id = 1;
    }
}

这篇关于Firebase值事件侦听器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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