3

My firebase database looks like this:

database

From this query

String currentUserID = FirebaseAuth
                            .getInstace()
                            .getCurrentUser()
                            .getUid();

DatabaseReference favorsRef = FirebaseDatabase
                                        .getInstance()
                                        .getReference()
                                        .child("favors");
Query myChatsQuery = favorsRef
                        .orderByChild("uid")
                        .equalTo(currentUserID);

I need to create other query to get just the tuples from myChatsQuery who has the child called "messages". If the tuple doesn't have the child "messages", I don't want to insert it in my new query.

So, How I can create a query from other query?

Help me please,

Thanks in advance.

2
  • If I get you correctly, you want to check which of those uids have the child called messages, and if they have you want to do something with it, right? Commented Oct 31, 2018 at 3:09
  • @PradyumanDixit exactly. First I check in favors those children who have the uid that I want (equals to currentUserID) because not all of them have the same uid. And then I need to check between all the children who have the uid=currentUserID which of those have a child called messages to do something with those. Commented Oct 31, 2018 at 3:47

2 Answers 2

1

Unfortunately, orderByChild() does not work on multiple same fields at once, so you may have to do the job in two steps.

First you can run an orderByChild() query looking for the currentUserId and then add all those that match to an ArrayList and then run orderByChild() in those found uids for messages.

This looks something like this, in code:

reference.orderByChild("uid").equalTo(currentUserId).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            if(dataSnapshot.exists())
                              array.add(dataSnapshot.getValue(String.class);
                        }

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

                        }
                  )};

This code above adds uids matching currentUserId to the ArrayList array.

databaseReference.child(array.get(0)).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                           if(dataSnapshot.child("messages").exists())
                           // do your thing

                        }

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

                        }

                    )};    

You can also just use a for loop to go through all the values in the array.

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately, this solution won't work because you cannot use .orderByChild("messages"). messages is not a property so you can order according to it. Furthermore, there is no need to add two listeners in order to verify the message node for existens.
Actually I need to work with the list resulting, so how can I implement the first solution w ArrayList?
So you provided the same exact answer as mine, right?
@AlexMamo yes, I think the answer almost matches yours, I am removing this code and giving another solution from the first approach. Apologies!
@esterrrsinH, glad to help. Cheers! :)
0

To solve this, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("favors").orderByChild("uid").equalTo(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            if(ds.child("messages").exists()) {
                //Do what you need to do
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

As you can see, you can solve this by querying the database only once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.