Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update and delete data in Firebase

This is my database:

{
  "UID1" : {
    "KEY" : {
      "Name" : "name1",
      "Email" : "[email protected]",
      "userid" : "UID1"
    }
  },
  "UID2" : {
    "KEY2" : {
      "Name" : "name1",
      "Email" : "[email protected]",
      "userid" : "UID2"
    },
    "KEY3" : {
      "Name" : "name2",
      "Email" : "[email protected]",
      "userid" : "UID2"
    },
    "KEY4" : {
      "Name" : "name3",
      "Email" : "[email protected]",
      "userid" : "UID2"
  }
}

I would like to update and delete data which for example would be at the position of "KEY2".

How can I achieve this? I have not yet tried any code in Android.

like image 260
Danny boy Avatar asked Jan 26 '17 08:01

Danny boy


People also ask

How do I delete data from Firebase?

The simplest way for deleting data is to call removeValue() on a reference to the location of that data. We can also delete data by specifying null as the value for another write operation such as setValue() or updateChildren().

How do I update my firestore data?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.


1 Answers

To write single data you can use the setValue() method on your DatabaseReference with your child Id's:

private void writeNewData(String userId, String name, String email) {
    User user = new User(name, email);    
    mDatabase.child("users").child(userId).setValue(user);
}

In your case you can do something like: mDatabase.child("UID2").child("KEY2").setValue(yourNewValueOrObject);

If you want to update a specific value, you should be more concise: mDatabase.child("UID2").child("KEY2").child("email").setValue(newEmail);

Anyway I recomend you to use custom classes as POJO's(Plain Old Java Object) with the values of each of your items in database. For example:

public class User {

    public String username;
    public String email;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String email) {
        this.username = username;
        this.email = email;
    }

}

Finally to remove data you should use the removeValue() method in the same way.

  private void deleteUserData(String userId) {           
        mDatabase.child("users").child(userId).removeValue();
    }

This method will remove the whole reference from your Database, so be care with it. In the case that you wanted to remove a specific field, you should add another .child() call to the tree. For example, let's say that we want to remove the email value from "KEY2" node: mDatabase.child("users").child(userId).child("email").removeValue();

Finally, there's the case that maybe we want to update multiple fields in different database nodes. In that case we should use the updateChildren() method with a map of references and values.

private void writeNewPost(String userId, String username, String title, String body) {
   // Create new post at /user-posts/$userid/$postid and at
   // /posts/$postid simultaneously
   String key = mDatabase.child("posts").push().getKey();
   Post post = new Post(userId, username, title, body);
   Map<String, Object> postValues = post.toMap();

   Map<String, Object> childUpdates = new HashMap<>();
   childUpdates.put("/posts/" + key, postValues);
   childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

   mDatabase.updateChildren(childUpdates);
}

What updateChildren method do. Is a setValue () call over each row in the given Map<String, Object> being the key the full reference of the node and the Object the value.

You can read more update and delete data in the official Firebase documentation

like image 98
Francisco Durdin Garcia Avatar answered Sep 20 '22 03:09

Francisco Durdin Garcia