Android Firebase update only some fields
Solution 1
The reference with which you are performing the update is for the path FB_REFERENCE_USER
- which appears to be the path which contains the users.
updateChildren
updates only the immediate children, with the values under those being replaced. So the update is replacing the entire child for the user and is leaving the children for the other users untouched. That is the expected behaviour.
You need to perform the update using a ref for that specific user - so that the properties of that user that are not being updated are left untouched.
That is, you need to do something like this:
User user = new User(userID, userName, userMail, userActive);
Map<String, Object> postValues = user.toMap();
myRef.child(userID).updateChildren(postValues);
The behaviour of updateChildren
is described here in the documentation.
Solution 2
if you want to update only some field, try to use
myRef.child(userID).child("which field do you want to update").setValue(ValueVariable);
Solution 3
Yes, you can update the single node without loss of other existing data.
You need to create a pojo
and set the values which you want to update in the existing node and send that pojo
object to setValue(pojo)
method.
For example:
User updatedUser=new User();
updatedUser.setName("Rishabh");
updatedUser.SetEmail("[email protected]");
myref.setValue(updatedUser);
PS: myref
is the reference up to the user id
node in which you want to update the data.
rodrigo
Updated on June 05, 2022Comments
-
rodrigo about 2 years
I'm developing an Android app and every time when the user launches this app, it will generate a new register (if not exist) from this user, on update the existing data, and my app will insert more information in the user node.
Every time I update this node, all others information are lost.
My user class
@IgnoreExtraProperties public class User { public String userID; public String userName; public String userMail; public boolean active; public User() {} public User(String userID, String userName) { this.userID = userID; this.userName = userName; } public User(String userID, String userName, String userMail, boolean active) { this.userID = userID; this.userName = userName; this.userMail = userMail; this.active = active; } @Exclude public Map<String, Object> toMap() { HashMap<String, Object> result = new HashMap<>(); result.put("user_id", userID); result.put("name", userName); result.put("email", userMail); result.put("active", active); return result; } }
And my update code:
public class FireBaseUser { public static String FB_REFERENCE_USER = "user"; public void updateUser(String userID, String userName, String userMail, boolean userActive) { // Write a message to the database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference(FB_REFERENCE_USER); User user = new User(userID, userName, userMail, userActive); Map<String, Object> postValues = user.toMap(); Map<String, Object> childUpdates = new HashMap<>(); childUpdates.put(userID, postValues); myRef.updateChildren(childUpdates); } }
When I call
myRef.updateChildren(childUpdates);
, all my old data are updated and my new children (that I created during the app) are lost. -
rodrigo over 7 yearsHi, thanks, i'm doing this and is all right. However, I try use a user class for a "clean" mode..
-
Aldi Renaldi Gunawan over 7 years@rodrigo you need to add all the data needed to your User classes.
-
user3833732 about 7 years@AldiRenaldiGunawan- This was perfect! But need your help again. This was perfect because from 8 feilds i only needed to update 1 feild, and this really saves execution time. However now i need to update only 3 feilds out of the 8feilds? any trick to update it with a smaller object that will contain only those 3 feilds??
-
DragonFire about 5 yearsIt looks like this partial update will require creating a new pojo class, because the original pojo class will accept all items. Isn't creating objects expensive memory wise. Should be just not use a map to update the children? Like my answer below
-
DragonFire about 5 years@user3833732 please see my answer below using a map