1

I am using google app engine + firebase database. I need to change some values in firebase database via app engine, but setValue() method does not work. No exceptions sent, i can read data, so service account is working nice. Here is the code im using:

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

  ref.addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
        public void onDataChange(DataSnapshot snapshot)
        {
            if (snapshot.exists())
            {
                for (DataSnapshot childSnap : snapshot.getChildren()) {
                    if (childSnap.child("Character").exists()) {
                        int energyCurrent;
                        int energyMax;
                        int energyObtain;
                        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();

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

Here is the scheme of my database if u need it:

    "Users" : {
        "NRPdi5gaaaaaHSfovegwg8XgFjZ2" : {
          "Character" : {
            energyCurrent : 0,
            energyMax : 100,
            energyObtain : 20,
            and other char values..
          }
        }
    } 
Maxgmer
  • 456
  • 8
  • 19
  • 2
    Set some breakpoints? – OneCricketeer Dec 21 '16 at 14:48
  • @cricket_007 i think no, because i dont know what is that.) – Maxgmer Dec 21 '16 at 15:12
  • @cricket_007 if u mean continue, break ot sth like that, then no, i didnt use it. – Maxgmer Dec 21 '16 at 15:13
  • 1
    Breakpoints. Points in your code that you can pause and debug your application. If not, just add else statements to all the if statements and print anything because it's clearly not entering one of them if nothing happens – OneCricketeer Dec 21 '16 at 15:18
  • @cricket_007 in the place of setValue listener i put else and else block was done, that means databaseError==null, and that means everything is ok and setValue was executed. wtf?) – Maxgmer Dec 21 '16 at 15:58
  • By the way, you only need to do `setValue(Math.min(energyMax, finalEnergy)`. But really, `energyObtain / 100` is probably 0 and you multiply that, so still zero, then add nothing to `energyCurrent`... – OneCricketeer Dec 21 '16 at 16:04

1 Answers1

4
energyObtain = (int) (long) childSnap.child("Character").child("energyObtain").getValue();

And

energyObtain : 20,

Then this just zeros out.

(energyMax * (energyObtain / 100));

So, solution... Make a proper floating point

energyMax * (energyObtain / 100.0)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245