2

I want to do x when FirebaseRef snapshot.Value == null. This works in the Unity Editor everytime but when I do this on the Android device, it fails every time. The app doesn't crash, it just doesn't do anything.

auth is working fine. database is working fine (I'm getting all other values when the values exist) but when the value doesn't exist, that's where I have the issue.

Here is my sudo code, any help would be appreciated.

ref.Child("Location/That/Doesnt/Exist").GetValueAsync().ContinueWithTask(Task => {
  if (task.IsFaulted){
    //Do something
  }
  else if (task.IsCompleted){
    DataSnapshot snap = task.Result;
    if(snap.Value == null){
      //This is what hangs. I get here and it just does nothing.
    }
  }
});

I'm so lost. I'm not sure how to figure this out. I get no errors but it's like the function just stops there.

Thanks all!

EDIT

So what I'm doing to get around this is to create a DB entry in the path with key == "0" and value == 0. Then I say if the ChildCount is greater than 1 and that child has more than 1 child, proceed.

I feel like this is a really poor approach.

KENdi
  • 7,576
  • 2
  • 16
  • 31
DKinnison
  • 355
  • 1
  • 3
  • 16
  • [Here is a simular javascript question](https://stackoverflow.com/questions/24824732/test-if-a-data-exist-in-firebase) with an Android answer. – André Kool Aug 07 '18 at 10:05
  • The answer suggests using HasChild(var), I have done this as well and have even tried using "ChildCount > x". Neither work on the device but both work in the Unity Editor. – DKinnison Aug 07 '18 at 13:46
  • Have you tried using the [exists](https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists) functionality? – André Kool Aug 07 '18 at 13:56
  • Yes I get the same result. All works in the editor but fails in the android build. – DKinnison Aug 07 '18 at 15:38

2 Answers2

1

I was getting the same error. I caught an exception of this, and i got NullReference, it's seems that on android when it's not found on database return Null, so you have to convert this:

if(snap.Value == null){ }

to this:

if (snap==null && snap.Value==null)

You are trying to access to snap.Value when snap object is null.

Sorry my english, i hope this helps you.

jesemu
  • 11
  • 1
  • Tried this and it did not work but it gave me another idea. I'm going to try another approach because I think you're on to something. For the record, I tried DataSnapshot snapshot = task.Result; if (snapshot == null && snapshot.Value == null) { popMNG.CreatePop("We are getting the empty location!", "Wa hoo!", "close", null); } – DKinnison Aug 08 '18 at 18:24
  • But, try the opposite, i mean, if you try if (snap==null && snap.Value==null) That wouldn't work, because you are accessing to snap.Value, when snap is null, try this: if (snap!=null && snap.Value!=null) { //something code } else { //what you want to do when it's null } – jesemu Aug 09 '18 at 08:24
0

try trailing slash

"Location/That/Doesnt/Exist/"

Anuj Jindal
  • 1,711
  • 15
  • 24
  • Trailing slash doesn't seem to do it. I get the same result. According to the Firebase docs, they don't use a trailing / either. FirebaseDatabase.DefaultInstance .GetReference("Leaders") .GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; // Do something with snapshot... } }); – DKinnison Aug 06 '18 at 20:20