0

I want to move Firebase realtime database child value into another child value in an android studio. ( points: 25 into totalpoints: from the same user uid.

{
  "Users" : {
    "8xNAsCfc3Afta2lPHHsMWHEOw8C2" : {
      "points" : 25,
      "users" : "umar"
    }
  },
  "wFinal" : {
    "8xNAsCfc3Afta2lPHHsMWHEOw8C2" : {
      "totalpoints" : 0
    }
  }
}

I want this

{
  "Users" : {
    "8xNAsCfc3Afta2lPHHsMWHEOw8C2" : {
      "points" : 0,
      "users" : "umar"
    }
  },
  "wFinal" : {
    "8xNAsCfc3Afta2lPHHsMWHEOw8C2" : {
      "totalpoints" : 25
    }
  }
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Ali Musa
  • 11
  • 3

2 Answers2

0

What you can do is at the time you do a .setValue(points) to your Users - userId reference, you also do a .setValue(points) at the wFinal Reference

Make sure to add a complete listener after .setValue() to know when each transaction completes.

Pseudocode

userRef.child(userId).child("points").setValue(0).addOnCompleteListener {

  onComplete(...) {
    wFinalRef.child(userId).child("totalPoints").setValue(points);
  } 

}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

As I said in comment, something like this.

FirebaseDatabase.getInstance().getReference().child("Users").child(userUid).child("points").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            long points = dataSnapshot.getValue(Long.class);
        //transfer the value
        FirebaseDatabase.getInstance().getReference().child("wFinal").child(userUid).child("totalpoints").setValue(points);
        //set the value at Users to 0
        FirebaseDatabase.getInstance().getReference().child("Users").child(userUid).child("points").setValue(0);
    }
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • Lots of love and special thanks for answering my question I have successfully implemented this method. – Ali Musa Feb 02 '20 at 18:30