Android Firebase updates only some fields

I am developing an Android application, and every time a user launches this application, it generates a new register (if it does not exist) from this user when updating existing data, and my application will insert additional information into the user node.

Every time I update this node, all other information is lost.

My custom 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 is updated, and my new children (which I created during the application) are lost.

enter image description here

+10
source share
4 answers

The link you are updating from is the path FB_REFERENCE_USER - which is the path that contains the users.

updateChildren updates only the immediate children, with the values ​​being overwritten. Thus, the update replaces the entire child for the user and leaves the children for other users intact. This is the expected behavior.

You need to upgrade using the ref link for that specific user, so that properties of that user that are not updated remain 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 behavior of updateChildren described here in the documentation.

+10
source

if you want to update only a certain field, try using

 myRef.child(userID).child("which field do you want to update").setValue(ValueVariable); 
+8
source

Yes, you can upgrade one node without losing other existing data.

You need to create a pojo and set the values ​​that you want to update on the existing node and send this pojo object to the setValue(pojo) method.

For instance:

 User updatedUser=new User(); updatedUser.setName("Rishabh"); updatedUser.SetEmail(" rishabh9393@gmail.com "); myref.setValue(updatedUser); 

PS: myref is a link to the user id node in which you want to update data.

+1
source

You can use the card to do the same ...

  String active = "true"; String email = " email@email.com "; String name = "your name"; String user_id = "user_id"; // HashMap For Updating Data HashMap<String, Object> updateFbDb = new HashMap<>(); updateFbDb.put("active", active); updateFbDb.put("email", email); updateFbDb.put("name", name); updateFbDb.put("user_id", user_id); fbDbRefRoot.child("user").child(currentUserId).updateChildren(updateFbDb).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { // Done } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.d(TAG, "fbSetValueFailure: " + e.getMessage()); } }); 
0
source

Source: https://habr.com/ru/post/1262905/


All Articles