我正在回答此问题然后我想到了这个新问题.
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?
发布评论