0

I'm following the guide here for removing values from my firebase database. Here is the structure of my data.

 |---users
     |----KJSXd4ScEmJ6N4UFc5k 
          |---bookmarks
              |---KdCyKdQkDg34ny6_H-C
                  |---id:"12d5j2fa-0f70-41c3-b4g4-4d66bdcef976"
              |---KdCyKdQkDg34ny6_H-M
                  |---id:"fa95b1fa-b537-4d98-a0e7-a92ffea7b6a4"

Here's the code I'm using.

FirebaseDatabase.getInstance().getReference().child(MyConstants.FIREBASE_USERS_NODE).child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(MyConstants.FIREBASE_BOOKMARKS_NODE).orderByChild("id").equalTo(uuid).addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        dataSnapshot.getRef().removeValue();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });

What's happening is the entire /bookmarks node gets deleted on this single call instead of just the reference to the bookmark wanted. How is it that I can achieve just deleting the single bookmark I want to delete instead of the whole node?

Community
  • 1
  • 1
Landen
  • 489
  • 1
  • 5
  • 8

2 Answers2

2

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

So in your case, the snapshot is not the item, but it's the total result of the query. Hence its getRef() returns the location on which you fired the query.

The solution is to loop over the results and delete each individually:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot child: dataSnapshot.getChildren()) {
        child.getRef().removeValue();
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • On additional followup question. If you have the option to add .orderBy() and .equalTo() but the result doesn't really return the only result, what's the point of using them? There's only one uuid for that bookmark in the dataset, yet the query returns the whole dataset? This seemed to be the answer, but I was looking for something within Firebase that would allow me to query better and not have to just return the whole dataset and then loop through it parsing the results. – Landen Feb 17 '17 at 22:26
  • Values don't have to be unique, so a query can match multiple results. The fact that in your specific case there's only one result, doesn't change the return type (that wouldn't be possible in most typed languages). – Frank van Puffelen Feb 18 '17 at 01:53
0

Why are you listening for the SingleValueEvent for that node, just try the below code.

FirebaseDatabase.getInstance().getReference().child(MyConstants.FIREBASE_USERS_NODE).child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(MyConstants.FIREBASE_BOOKMARKS_NODE).child(uuid).removeValue();
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46