本文介绍了Firebase阅读问题。对象始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,应该初始化我的字段变量 currentTask 。我不明白为什么我不能从firebase读取我的对象。这里是方法:

  private void getCurrentTask(){
final DatabaseReference dRef1 = database.getReference()。child ( 用户)子(UID).child( CurrentTask);
dRef1.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()){
currentTask = dataSnapshot.getValue(CurrentTask.class);
} else {
Toast toast = Toast.makeText(TasksListActivity.this,no magic,Toast.LENGTH_LONG);
toast.show() ;
}
}

@Override
public void onCancelled(DatabaseError databaseError){}
});

if(currentTask == null){
Toast Toast = Toast.makeText(this,magic),Toast.LENGTH_LONG);
toast.show();




Toast 没有魔法 code>永远不会出现,所以对象存在。但是,在方法结束的时候出现 magic ,这意味着 currentTask == null 。即使在初始化!



这里是我的数据库:

解决方案

和的答案是正确的。但是,通过描述你的代码如何工作,让我更简单一些:

  private void getCurrentTask(){ 
...
dRef1.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
...(point 1)
}

@Override
public void onCancelled(DatabaseError databaseError){}
});

...(点2)
}

数据库异步加载您的数据。这意味着(使其简单)获取数据的过程不会干扰您的主要过程。考虑到这一点,(point 2)中的代码并不总是在(point 1) ,反之亦然。虽然在(point 1)(point 2)后执行的代码通常会被执行,但并不总是这样。所以你应该考虑在(point 1)中写的代码可以在任何时候执行。

然后你应该用这个概念写你的代码。这意味着如果你想对(point 1)中的变量做任何事情(比如用dataSnapshot值填充currentTask然后检查它是否为null),你应该把它放在里面(point 1)



希望这个帮助。

I have a method which should initialize my field variable currentTask. I don't understand why I can't read my object from firebase. Here is the method:

private void getCurrentTask() {
    final DatabaseReference dRef1 = database.getReference().child("Users").child(uid).child("CurrentTask");
    dRef1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                currentTask = dataSnapshot.getValue(CurrentTask.class);
            } else {
                    Toast toast = Toast.makeText(TasksListActivity.this, "no magic", Toast.LENGTH_LONG);
                    toast.show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

    if (currentTask == null) {
        Toast toast = Toast.makeText(this, "magic)", Toast.LENGTH_LONG);
        toast.show();
    }
}

Toast no magic never appears, so the object exists. But, in the end of the method toast magic appears, which means currentTask == null. Even after initialization!

And here is my database:

解决方案

@Roasario and @ReazMurshed 's answer is both right. But let me make it more simple by describing how your code doesn't work as you expected:

private void getCurrentTask() {
    ...
    dRef1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ... (point 1)
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

    ... (point 2)
}

Firebase Database load your data asyncronously. It means (to make it simple) that the process of getting data does not interfere with your main process. With that in mind, the code in (point 2) does not always get executed after code in (point 1), and vice versa. Though usually code in (point 1) get executed after (point 2), it is not always like that. So you should consider that the code you write in (point 1) can be executed at anytime.

Then you should write your code with that concept. Meaning that if you want to do anything to a variable inside (point 1) (like filling currentTask with dataSnapshot value then check if it is null), you should place it all inside (point 1)

Hope this help.

这篇关于Firebase阅读问题。对象始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:30