Firebase Database Overwrites Previous Value With Setvalue()
So I have a Database in Firebase. This code is in onCreate(): DatabaseReference mDatabaseRefUser =FirebaseDatabase.getInstance().getReference('Users'); This code is in another met
Solution 1:
To update a value instead of overriding it, you should use updateChildren() method.
You are overriding it because every node in a Firebase database is a Map
and in the case of Map
, it replaces the old value with the new one.
Solution 2:
To update data you should go for such kind of code instead of using child.setValue()
as its name suggests it will directly replace old values with new values. But If you want to change a specific value then you should go for something as below. Give it a try mate!
HashMap<String, Object> params = new HashMap<>();
params.put("yourKey", your value);
yourDatabaseChildReference.updateChildren(params);
Solution 3:
My only solution to this issue was to make a new Database,
I added this:
mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
.child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
mAuth.getCurrentUser().getEmail()));
mDatabaseRefUser.child("Chatting").child(userID)
.child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
userEmail));
and removed the old one
Post a Comment for "Firebase Database Overwrites Previous Value With Setvalue()"