Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - Merging two queries locally

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

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).

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
                                    }
                                }
                            });
                }
            });
}
like image 434
Florian Walther Avatar asked May 01 '18 14:05

Florian Walther


People also ask

What is querySnapshot in firebase?

A QuerySnapshot contains the results of a query. It can contain zero or more DocumentSnapshot objects. Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.

How to combine two queries in Firestore?

You can achieve this, using the following lines of code: FirebaseFirestore rootRef = FirebaseFirestore. getInstance(); Query firstQuery = rootRef... Query secondQuery = rootRef...

How do I aggregate data in firestore?

If you want to gain insight into properties of the collection as a whole, you will need aggregation over a collection. Cloud Firestore does not support native aggregation queries. However, you can use client-side transactions or Cloud Functions to easily maintain aggregate information about your data.


1 Answers

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
    }
});

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.

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.

like image 136
Alex Mamo Avatar answered Sep 27 '22 22:09

Alex Mamo