Firestore - 在本地合并两个查询 [英] Firestore - Merging two queries locally

查看:91
本文介绍了Firestore - 在本地合并两个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Firestore中没有逻辑 OR 运算符,因此我尝试在本地合并2个单独的查询。

Since there is no logical OR operator in Firestore, I am trying to merge 2 separate queries locally.

现在我想知道如何保持结果的正确顺序。当我独立运行2个查询时,我无法特定地查询结果(至少不是我使用 orderBy 方法从Firestore获取结果的顺序)。

Now I wonder how I can keep up the proper order of the results. When I run 2 queries independently, I can't oder the results specificly (at least not the order in which I get the results from Firestore with the orderBy method).

我的想法是将第二个查询放在第一个查询的 onSuccessListener 中。这是一个糟糕的主意吗?

My idea was to put the 2nd query inside the onSuccessListener of the 1st query. Is this a bad idea performance wise?

public void loadNotes(View v) {
    collectionRef.whereLessThan("priority", 2)
            .orderBy("priority")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        Note note = documentSnapshot.toObject(Note.class);
                        //adding the results to a List
                    }

                    collectionRef.whereGreaterThan("priority", 2)
                            .orderBy("priority")
                            .get()
                            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                @Override
                                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                        Note note = documentSnapshot.toObject(Note.class);
                                        //adding the results to a List
                                    }
                                }
                            });
                }
            });
}


推荐答案

在本地合并2个单独的查询,我建议你使用 Tasks.whenAllSuccess()方法。您可以使用以下代码行来实现此目的:

To merge 2 separate queries locally, I recommend you to use Tasks.whenAllSuccess() method. You can achieve this, using the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query firstQuery = rootRef...
Query secondQuery = rootRef...

Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();

Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
         //Do what you need to do with your list
    }
});

如你所见,当覆盖 onSuccess()时方法结果是列表对象,其具有作为参数传递到 whenAllSuccess()的任务的确切顺序方法。

As you can see, when overriding the onSuccess() method the result is a list of objects which has the exact order of the tasks that were passed as arguments into the whenAllSuccess() method.

还有另一种方法,那就是使用 Tasks.continueWith()方法。但根据你的应用程序的用例,你可以使用eiter whenAllSuccess()方法或 continueWith()方法。请在此处查看官方文档

There is also another approach and that would be to use Tasks.continueWith() method. But according to the use-case of your app, you can use eiter whenAllSuccess() method or continueWith() method. Please see here the official documentation.

这篇关于Firestore - 在本地合并两个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆