DataSnapshot值是否有可能为null? [英] Is there any chance DataSnapshot value is null?

查看:49
本文介绍了DataSnapshot值是否有可能为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在回答此问题然后我想到了这个新问题.

I am answering this question then this new question come to my mind.

在该问题中,我将这样描述示例代码:

In that question, I describe my example code like this:

boolean firstCallDone = false;
boolean secondCallDone = false;

DataSnapshot firstDataSnapshot = null;
DataSnapshot secondDataSnapshot = null;

onCreate() {
    firstRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            firstCallDone = true;
            firstDataSnapshot = dataSnapshot;

            if (firsCallDone && secondCallDone)
                doSomething();
        }
        ...
    }
    secondRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            secondCallDone = true;
            secondDataSnapshot = dataSnapshot;

            if (firsCallDone && secondCallDone)
                doSomething();
        }
        ...
    }
}

doSomething() {
    // do something with firstDataSnapshot and secondDataSnapshot
    ...
}

以上代码的目的是在调用两个Firebase数据库并获取其值之后执行 doSometing().

The purpose of above code is to execute doSometing() after two Firebase Database has been called and its values gotten.

然后我意识到,我可以像这样 if(firstDataSnapshot!= null& secondampSnapshot!)来验证它,而不是像 if(firsCallDone& secondampDone)那样进行验证.= null).据我所知,这是因为DataSnapshot 从不为null(将其值填充在 onDataChange()内部)之后.

Then I realize, instead of validating like this if (firsCallDone && secondCallDone), I can validate it like this if (firstDataSnapshot != null && secondDataSnapshot != null). That is because as far as I know, DataSnapshot never null (after its value has been filled inside onDataChange()).

但是我不确定.在 onDataChange()中,DataSnapshot是否有可能为null?如果有,那是什么情况呢?

But I am not sure. Is there any chance that DataSnapshot be null inside onDataChange()? If there is, what is the case that make it happen?

推荐答案

DataSnapshot永远不能为null,无论那里是否有数据(因为如果没有数据,您将获得一个空但非空的DataSnapshot数据).

The DataSnapshot can never be null, regardless of whether there is data there or not (since you'll get an empty but non-null DataSnapshot if there is no data).

这篇关于DataSnapshot值是否有可能为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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