Maxgmer
Maxgmer

Reputation: 466

Reading and writing data in Firebase

I need to increment energy values of each user in my Android app.

Here is the scheme of my database:

"Users" : {
    "NRPdi5gzbsWoHSfovOtIO8XgFjZ2" : {
      "Character" : {
        energyCurrent : 0,
        energyMax : 100,
        energyObtain : 20,
        and other char values..
      }
    }
} 

I read, do some calculations and write data with this code, but nothing changes in my Firebase database. This code is on Google App Engine. No exceptions sent, just nothing happens.

final FirebaseDatabase db = FirebaseDatabase.getInstance();
            DatabaseReference ref = db.getReference("Users");

            ref.addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot childSnap : snapshot.getChildren()) {
                        int energyCurrent;
                        int energyMax;
                        int energyObtain;
                        try {
                            energyCurrent = (int) childSnap.child("Character").child("energyCurrent").getValue();
                            energyMax = (int) childSnap.child("Character").child("energyMax").getValue();
                            energyObtain = (int) childSnap.child("Character").child("energyObtain").getValue();
                        }
                        catch (Exception e)
                        {
                            continue;
                        }

                        float finalEnergy = energyCurrent + (energyMax * (energyObtain/100));
                        if (finalEnergy > energyMax){
                            DatabaseReference reference = childSnap.getRef().child("Character").child("energyCurrent");
                            reference.setValue(energyMax, new DatabaseReference.CompletionListener() {
                            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                throw databaseError.toException();
                            }
                        });
                        }
                        else
                        {
                            DatabaseReference reference = childSnap.getRef().child("Character").child("energyCurrent");
                            reference.setValue((int)finalEnergy, new DatabaseReference.CompletionListener() {
                            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                throw databaseError.toException();
                            }
                        });
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            });

Upvotes: 1

Views: 927

Answers (2)

Aldi Renaldi Gunawan
Aldi Renaldi Gunawan

Reputation: 510

you cannot be cast integer in long variable, if u insist to use that, cast :

try {
     energyCurrent = (int)(long) childSnap.child("Character").child("energyCurrent").getValue();
     energyMax = (int)(long) childSnap.child("Character").child("energyMax").getValue();
     energyObtain = (int)(long) childSnap.child("Character").child("energyObtain").getValue();
 }

and you should remove :

throw databaseError.toException();

on the OnComplete Database

Upvotes: 1

Bob Snyder
Bob Snyder

Reputation: 38289

The following statement throws an exception for every iteration of your loop:

energyCurrent = (int) childSnap.child("Character").child("energyCurrent").getValue();

getValue() on a numeric string returns a Long, which cannot be unboxed to int, and instead throws:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

The continue statement in the catch-block transfers control to the end of the loop's body, skipping the code containing the calls to setValue().

You can use a double cast for the three statements:

int energyCurrent = (int)(long) dataSnapshot.child("count").getValue();

Or make your variables longs and use a single (long) cast:

long energyCurrent;
long energyMax;
long energyObtain;

Upvotes: 1

Related Questions