Ad

How To Find The Specified Data As Described In Given JSON Tree In Firebase Realtime Database?

In the following JSON Structure of Friend Requests "LONiSEYOArTUAYF2CJ2zf6EzpqC" is the current user id. It has nested user ids of users from which he has received requests or he has sent a request to. Now I want to get the userIds of all the nested users who have sent a request to the current user with id "LONiSEYOArTUAYF2CJ2zf6EzpqC" in Android.

I have tried the following code in Android.

Database Reference mRequestDatabase = FirebaseDatabase.getInstance().getReference().child("Requests").child(currentUserId);

Query query = 
mRequestDatabase.orderByChild("request_type").equalTo("received");

But this Query doesn't work as database reference is to the currentUserId. How to loop through all the users under current User Id and check which have request_type = "received"?

{
  "Requests" : {

     "LONiSEYOArTUAYF2CJ2zf6EzpqC":{
        "anicGIuuLSQndxMKI8ieZDhMHOP":{
          "request_type" : "received"
        },

        "TIh83WLasxYxLxJmUh619imfnrd" :{
          "request_type" : "received"
        }

      } 
      ,
      "LioCIXTY8_Cgii2TtHb" : {
         "NthWyK6XoLdAOZCdmkJXIGS3YuR2" : {
           "request_type" : "received"
         }

      }
      ,
      "91cGZGssoTQOoepy" :{

        "NthWyK6XoLdAOZCdmkJXIGS3YuR2" : {
           "request_type" : "send"
         }


      }


    }

}
Ad

Answer

Your query seems fine. If you want to get keys of the requests with request_type equals to "received" for the current user, you can get the keys as follows:

DatabaseReference requestDatabase = FirebaseDatabase.getInstance().getReference()
        .child("Requests")
        .child(currentUserId);

final List<String> senderUserIds = new ArrayList();

requestDatabase.orderByChild("request_type").equalTo("received")
        .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot requestSnapshot : dataSnapshot.getChildren()) {
            senderUserIds.add(requestSnapshot.getKey());
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

RECOMMENDATION

If you are able to change the structure of the database, you can flatten it like this:

{
    "Requests": {
        "request_id_1": {
            "sender_user_id": "user_1",
            "recipient_user_id": "user_2"
        }
    }
}

Then, you may query as follows:

DatabaseReference requestsRef = FirebaseDatabase.getInstance().getReference().child("Requests");

final List<String> senderUserIds = new ArrayList();

requestsDatabase.orderByChild("recipient_user_id").equalTo(currentUserId).addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
            Request request = itemSnapshot.getValue(Request.class);
            senderUserIds.add(request.getSenderUserId());
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Here is the Request class:

public class Request {

    private String senderUserId;
    private String recipientUserId;

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

    Request(String senderUserId, String recipientUserId) {
        this.senderUserId = senderUserId;
        this.recipientUserId = recipientUserId;
    }

    public String getSenderUserId() {
        return senderUserId;
    }

    public void setSenderUserId(String senderUserId) {
        this.senderUserId = senderUserId;
    }

    public String getRecipientUserId() {
        return recipientUserId;
    }

    public void setRecipientUserId(String recipientUserId) {
        this.recipientUserId = recipientUserId;
    }
}
Ad
source: stackoverflow.com
Ad