如何在Firestore中合并任务以创建AND查询?

时间:2018-06-30 00:28:37

标签: android firebase google-cloud-firestore

这是针对OR个查询任务

请告诉我如何将其转换为AND查询?

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    CollectionReference learnings = db.collection("learnings");

    Task task1 = learnings.whereLessThan("l_fee", MaximumRequiredPrice)
            .get();

    Task task2 = learnings.whereLessThan("l_duration", MaximumRequiredDuration)
            .get();

    Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2);
    allTasks.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() {
        @Override
        public void onSuccess(List<QuerySnapshot> querySnapshots) {
            for (QuerySnapshot queryDocumentSnapshots : querySnapshots) {
                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    Learning learning = documentSnapshot.toObject(Learning.class);
                    result.add(learning);
                    adapter.notifyDataSetChanged();

                }
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

我在这里看到的问题是,当您只需要一个任务并添加了'where'子句时,就有两个任务:

FirebaseFirestore db = FirebaseFirestore.getInstance();

CollectionReference learnings = db.collection("learnings");

Task task1 = learnings.whereLessThan("l_fee", MaximumRequiredPrice)
        .whereLessThan("l_duration", MaximumRequiredDuration)
        .get();