Frigyes Vass
Frigyes Vass

Reputation: 311

Multiple QUERY filters on Firebase Collection

I want to apply multiple query filters on a Firestore collection but I have no idea how to do it. If I apply one filter, it works fine.

Part of my code:

private ArrayList<TaskItem> taskList;
private CollectionReference tasks;

taskList = new ArrayList<>();
tasks = firestore.collection("Tasks");

queryData();

private void queryData(){
        taskList.clear();

        tasks.whereEqualTo("recipient", user.getEmail())
                .whereNotEqualTo("status", "Completed")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for(QueryDocumentSnapshot document : queryDocumentSnapshots){
                            TaskItem item = document.toObject(TaskItem.class);
                            taskList.add(item);
                        }

                        if(taskList.size() == 0){
                            initalizeData();
                            queryData();
                        }

                        adapter.notifyDataSetChanged();
                    }
                });
    }

Upvotes: 0

Views: 122

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

When you want to filter documents from Firestore using whereEqualTo() together with whereNotEqualTo(), an index is required. To create such an index, please see my answer from the following post:

Once you create the index, you'll be able to map the documents from Firestore into objects of type "TaskItem" and add them to the adapter.

Upvotes: 1

Related Questions