1

I am trying to update a value in my Firebase database. This is the structure of the database:

enter image description here

I need to update the value of status to "accepted" if the r_id and s_id have a specific value. The problem here is that the key for each friend_data child is generated using "push" like this

db.child("friend_data").push().setValue(friend);

I have tried this question here and this one here but both don't meet my requirements. How do I go about solving this one?

Edit: I understand now that I am supposed to use a Query here. This is what I tried:

final DatabaseReference db = FirebaseDatabase.getInstance().getReference("friend_data");

Query query = db.orderByChild("r_id").equalTo(f_id).orderByChild("s_id").equalTo(id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            db.child(child.getKey()).child("status").setValue("accepted");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

This does not work either because this error is raised

java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!.

So how do I combine two Queries?

Ajil O.
  • 6,562
  • 5
  • 40
  • 72

1 Answers1

2

I have finally got this to work after days of trying to figure it out.

I added a Query with orderByChild method to filter out a big chunk of data at the server side itself (thanks to this answer for the idea.) The remaining data which I got as child of data type DataSnapshot had all the necessary information that I needed. Here is the query

Query query = db.orderByChild("r_id").equalTo(f_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            String child_s_id = (String) child.child("s_id").getValue();
            String child_status = (String) child.child("status").getValue();

            if (child_s_id.equals(id)) {
                Log.e("Got value", f_id + " - x -" + id + " " + child_status);

            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

But again I could not meet my requirements, because I actually needed to modify one of the value in the child and I could not use any methods like setValue to change the value of child.

Again, it took me a long while (what an idiot) to figure out that I had to convert the child (of data type DataSnapShot) to a reference for setValue to work. And it worked

child.getRef().child("status").setValue("accepted");

Here is the completed code. Hope it helps someone along the way

Query query = db.orderByChild("r_id").equalTo(f_id);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            String child_s_id = (String) child.child("s_id").getValue();
            String child_status = (String) child.child("status").getValue();

            if (child_s_id.equals(id))
                child.getRef().child("status").setValue("accepted");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Ajil O.
  • 6,562
  • 5
  • 40
  • 72