1

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
    ...
}

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

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()).

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?

Community
  • 1
  • 1
koceeng
  • 2,169
  • 3
  • 16
  • 37

2 Answers2

2

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).

Michael Lehenbauer
  • 16,229
  • 1
  • 57
  • 59
  • For others that are wondering and looking for answer to this question, also [check this answer](http://stackoverflow.com/a/41513071/4112725) – koceeng Jan 08 '17 at 03:58
1

No, the DataSnapshot value will never be null.

If any failure occurs, onCancelled() method will be triggered instead.

By the way, instead of creating flags to make sure that each listener is finished, you should instead take advantage of Tasks API to manage these tasks. This answer from a Firebaser explains about this and also provides a class for that.

Cheers :)

Community
  • 1
  • 1
Wilik
  • 7,630
  • 3
  • 29
  • 35
  • 1
    Hey Wilik: the DataSnapshot will never be null, not even when there is no value. `DataSnapshot.getValue()` may return `null`, but the snapshot itself will always be a valid object. That's why you can for example call `snapshot.exists()` on it, to see if there is a value at the location you're querying. – Frank van Puffelen Jan 07 '17 at 00:56
  • Thank you for the correction @FrankvanPuffelen. What I'm referring as "`DataSnapshot`'s value" in my answer is indeed `DataSnapshot.getValue()` but I just realized that it's not what the asker meant after I re-read the question. I will edit my answer. :) – Wilik Jan 07 '17 at 03:06
  • Yeah. I was also on my way to provide an answer based on `getValue()`, when I realized the question is about the snapshot object itself. Hence I pulled Michael in for an even more authoritative answer. :-) – Frank van Puffelen Jan 07 '17 at 04:34