DataSnapshot值是否有可能为null?

编程入门 行业动态 更新时间:2024-10-28 12:29:04
本文介绍了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?

本文发布于:2023-10-22 22:09:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有可能   DataSnapshot   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!