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

查看:21
本文介绍了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() 方法时,结果是一个 list 对象,这些对象具有作为参数传递到的任务的确切顺序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() 方法.但是根据您的应用程序的用例,您可以使用 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天全站免登陆