Home > Software design >  Save button deletes previous data in firebase
Save button deletes previous data in firebase

Time:06-13

I am new to android studio and firebase. I am trying to save a list of people to firebase like this. Idea is that the logged in user should be able to save information about some people.


String userId = user.getCurrentUser().getUid();
databaseReference.child("users").child(userId).child("savedPersons").child("name").setValue(nameTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("surname").setValue(surnameTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("gender").setValue(genderTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("ageTxt").setValue(ageTxt);

It does not surprise me that it deletes the previous saved person when i save another one but i don't know how to save all of them. I have this in my firebase but i need multiple saved users. How do i do it ? Firebase screenshot

CodePudding user response:

If you want to save multiple people in a list in the database, you'll want to call push:

String userId = user.getCurrentUser().getUid();
DatabaseReference newRef = databaseReference.child("users").child(userId).child("savedPersons").push(); //            
  • Related