Firebase 读取问题.对象始终为空 [英] Firebase reading issue. Object always null

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

问题描述

我有一个方法可以初始化我的字段变量 currentTask.我不明白为什么我无法从 firebase 读取我的对象.方法如下:

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 永远不会出现,所以对象存在.但是,在方法的最后出现了toast magic,表示currentTask == null.即使在初始化之后!

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!

这是我的数据库:

推荐答案

@Roasario@ReazMurshed 的答案都是正确的.但是让我通过描述您的代码如何不像您预期​​的那样工作来使其更简单:

@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 数据库异步加载您的数据.这意味着(简单来说)获取数据的过程不会干扰您的主过程.考虑到这一点,(point 2) 中的代码并不总是在 (point 1) 中的代码之后执行,反之亦然.尽管(point 1)中的代码通常在(point 2)之后执行,但并不总是这样.所以你应该考虑到你在(point 1)中编写的代码可以随时执行.

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.

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

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)

希望对您有所帮助.

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

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