How to save zero as a Double in firebase database

Viewed 2691

In my database, I'm keeping track of the distance travelled by users and set an initial value of 0 for everyone. In my code, I am updating this value by getting the old value and adding the new distance to it, then saving that value.

At first, when I retrieve the 0 from the database, it is returned as a Long, which is fine:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    Long previousDistance = (Long) dataSnapshot.child("distance").getValue();
    userReference.child("distance").setValue(previousDistance.doubleValue()+distanceTravelled);
 }

The problem is that now, the value stored in the database is a Double, so next time the above code is run it will fail with a ClassCastExcepiton.

I could probably solve the issue with an if statement, but the easiest solution would be to store the initial 0 as a Double instead. However, I can't make that work. I've tried both of these:

myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(Double.valueOf(0.0));
myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(0.0);

They still get stored as "0" and returned as a Long. Is it possible to store zero as a Double in the database?

4 Answers

It isn't stored as "0" (String) it is stored as the number 0. In Firebase, it doesn't matter what kind of primitve you are storing, as long as it is a 0 (zero). If you add to an existing value of 0 (zero), 0.01, the result will be: 0.01, which obviously is a Double.

One thing to remember is to get the data from your database in the same wasy you are storing it. If you are setting a value as a Double using this line of code:

myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(0.01);

If you want only a 0 (zero), you can only use:

myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(0);

And to get it back, you need to use this line of code:

double previousDistance = dataSnapshot.child("distance").getValue(Double.class);
double previousDistance = (double) dataSnapshot.child("distance").getValue();

And you should put it as normal:

myDatabase.child("users").child(lowerCaseUserName).child("distance").setValue(0);

I guess it is evaluated as a zero no matter if there is a floating point in the number or not so you could either add a close to zero value instead, but with a floating point that have sense. Depending on your required precision this won't hurt.

...child("distance").setValue(0.0000000001);

Otherwise storing it as a String and converting back to double everytime you use the value.

I could not find another way of doing that neither, it auto-cast to Long automatically as you said...

Remember that firebase is a JSON tree node based system. You should be using wrappers all along to serialize it to JSON. JSON only supports long as numerical values to firebase, that's why the exception is happening over and over.

It's a good practice implement a method un the form of SerializeDoubleToString() and the inverse DeserializeStringToDouble().

Store Doubles as strings; you have a huge impact in the resolution of your numbers, Unity is mainly floating point 32b while firebase 64.

Related