Reading and writing data in Firebase
<p>I need to increment energy values of each user in my Android app.</p>

<p>Here is the scheme of my database:</p>

<pre><code>"Users" : {
    "NRPdi5gzbsWoHSfovOtIO8XgFjZ2" : {
      "Character" : {
        energyCurrent : 0,
        energyMax : 100,
        energyObtain : 20,
        and other char values..
      }
    }
} 
</code></pre>

<p>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.</p>

<pre><code>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 &gt; 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();
                }
            });
</code></pre>