2

I am using Cloud Firestore in an Android app.

I create 2 queries, one whereLessThan and one whereGreaterThan to effectively create an != query. I order the results for each individual query, but when I run them separately, I might get one or another returned first, depending on which is faster.

When I combine 2 queries into 1 and then use the Tasks.whenAllSuccess method to get 1 Task and add 1 OnSuccessListener to it, will it keep up the order of results in which I pass my tasks?

public void loadNotes(View v) {
    Task task1 = notebookRef
            .whereLessThan("priority", 2)
            .orderBy("priority")
            .get();

    Task task2 = notebookRef
            .whereGreaterThan("priority", 2)
            .orderBy("priority")
            .get();

    Task finalTask = Tasks.whenAllSuccess(task1, task2).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
        @Override
        public void onSuccess(List<Object> objects) {
            //will my results be ordered prioty 1 - 3 - 4 - 5....?
        }
    });
}
Florian Walther
  • 6,237
  • 5
  • 46
  • 104

1 Answers1

3

Tasks.whenAllSuccess (and other whenAll methods) will deliver the results from the tasks to the callback in a List in the order they were passed to whenAllSuccess. If you need some other way of getting the results, you can maintain some data structure with the original Task objects, then query them as you want.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you. Is this the appropriate way to merge multiple queries? There is almost no documentation about it. I've been searching for hours. – Florian Walther May 01 '18 at 21:07
  • If you want a single point of contact for the completion of a collection of tasks, this is the right way to do it. See also this multi-part blog series. https://firebase.googleblog.com/2016/09/become-a-firebase-taskmaster-part-1.html – Doug Stevenson May 01 '18 at 21:08
  • Thanks for the link. I see this is for the Realtime Database, but I guess it also applies to Firestore? I wish there was more exact documentation, I stumpled upon these methods by accident. – Florian Walther May 01 '18 at 21:27
  • This is for all Firebase and Play services libraries that use Tasks. https://developers.google.com/android/guides/tasks – Doug Stevenson May 01 '18 at 21:34
  • Thanks, that looks helpful. But it seems that continueWith is the better approach to chain 2 tasks. What do you think? – Florian Walther May 02 '18 at 06:49
  • Whatever suits your needs. – Doug Stevenson May 02 '18 at 06:50
  • But I am still suspicious if whenAllSuccess actually guarantees to return the results in a particular order. They just write it gets triggered when all tasks are successful – Florian Walther May 02 '18 at 06:51
  • That's the whole point of whenAll methods - to get notified when **everything** is complete. – Doug Stevenson May 02 '18 at 06:53
  • 1
    Yea, but the important part is that I get the results in the correct order, not just getting them when they are all complete. But I didn't read the whole blog yet, maybe the answer is in there. – Florian Walther May 02 '18 at 07:14
  • The List in the callback has them in the order they were given as arguments. That much was previously established in my answer. – Doug Stevenson May 02 '18 at 07:15
  • Ok sorry that I didn't get it. Thank you. – Florian Walther May 02 '18 at 07:22
  • 1
    Can I ask 2 more things? The OnCompleteListener returns a List of Objects. Can I just cast these objects into QuerySnapshots? And the continueWith method waits until 1 tasks is finished, right? So if I want to execute both Tasks in parallel, it's better to not use it? – Florian Walther May 02 '18 at 09:37
  • SO is not appropriate for extended conversations. If you have followup questions, please post them as separate questions here, so that they can be easier to to search and that others may benefit. – Doug Stevenson May 02 '18 at 16:13
  • @FlorianWalther So, did you find the answer to your 2 more questions? – Shahood ul Hassan Jan 24 '22 at 03:10
  • @FlorianWalther So, did you find the answer to your 2 more questions? – Shahood ul Hassan Jan 24 '22 at 03:10