2

This answer touches on how to merge two queries together and I suppose this could be done for any number of known queries. However, I have an array of values I want to use to filter for documents that contain a field with those values(It is the same field for all values). The method linked above does not seem sufficient for this purpose.

I tried creating an array of queries using the new operator but this does not work since Query is private.

Query query = db.collection("posts")
                .whereEqualTo("userId", followingList)//followingList is a list of Strings that I want to query
                .orderBy("imageUrl_1", Query.Direction.ASCENDING);

This does not work since followingList is not a String

How I tried to create an array of Queries that does not work:

ArrayList<Query> list = new ArrayList<Query>();
        for(String s : followingList){
            list.add(new Query());//does not compile
        }

enter image description here This image of my Firestore shows how followingList is created. It is updated when a user clicks follow on the post of another user. What I want to be able to do is to create a query of all posts(contained in a separate collection) that contain the userIs's contained in followingList

David
  • 769
  • 1
  • 6
  • 28
  • 4
    Given that the linked answer depends on `Tasks.whenAllSuccess()`, it sounds like it would work with any number of tasks. If that doesn't work for you, show what you tried with the array of queries. Your existing code shows correctly how to create a single query. Creating an array of them, is just more of that. – Frank van Puffelen Jul 03 '19 at 21:16
  • But what if I don't know the number of tasks that should be passed? – David Jul 03 '19 at 21:18
  • 4
    The number is not important, so I'm not sure I understand. At some point you should know that you've found all tasks, and at that point you can pass them into `Tasks.whenAllSuccess()`. Note that `Tasks.whenAllSuccess()` doesn't care whether certain tasks may already have been completed, or whether some haven't even started yet. It handles such edge cases gracefully behind the scenes. – Frank van Puffelen Jul 03 '19 at 21:48
  • @frank In the linked example queries and tasks are created directly in the code. If I do not know how many I will have to create how can I do that? – David Jul 03 '19 at 21:55
  • I have no idea what the problem is at this point. The code you shared creates an array of queries. I you call [`get()`](https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/Query.html#get()) on each query, you can create a list of tasks that you then pass into `Tasks.whenAllSuccess(...)`. If you're having a hard time making that work, show what you've tried to create the list of tasks, so the non-working code. – Frank van Puffelen Jul 04 '19 at 05:40

1 Answers1

1

I've tested this on my current project, it works fine (tested for only two documents with a followingList.size of 2 elements)

public void testMy() {
        db = FirebaseFirestore.getInstance();
        List<String> followingList = new ArrayList<>();
        followingList.add("tag1"); followingList.add("tag2"); followingList.add("tag3"); // ...
        List<Task> taskList = new ArrayList<>();

        for (String s : followingList) {
            Query query = db.collection("invites")
                    .whereEqualTo("uid", s)
                    .orderBy("imageUrl_1", Query.Direction.ASCENDING);; //followingList is a list of Strings that I want to query
            Task<QuerySnapshot> task = query.get();
            taskList.add(task);
        }

        Tasks.whenAllSuccess(taskList.toArray(new Task[taskList.size()])).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
            @Override
            public void onSuccess(List<Object> objects) {
                @SuppressWarnings("unchecked")
                List<QuerySnapshot> querySnapshots = (List<QuerySnapshot>)(List<?>) objects;
                for (QuerySnapshot qs : querySnapshots) {
                    for (QueryDocumentSnapshot qds : qs) {
                        Log.d(TAG, qds.getData().toString());
                    }
                }
            }
        });
    }
Teempy
  • 491
  • 4
  • 11