Tasks.whenAllSuccess是否保证我将任务传递给它的顺序? [英] Does Tasks.whenAllSuccess guarantee the order in which I pass tasks to it?

查看:65
本文介绍了Tasks.whenAllSuccess是否保证我将任务传递给它的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android应用中使用Cloud Firestore.

I am using Cloud Firestore in an Android app.

我创建2个查询,一个whereLessThan和一个whereGreaterThan,以有效地创建一个!=查询. 我为每个查询排序结果,但是当我分别运行它们时,我可能会首先返回一个或另一个,这取决于哪个更快.

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.

当我将2个查询合并为1,然后使用Tasks.whenAllSuccess方法获得1个Task并向其添加1个OnSuccessListener时,它将保持我通过任务的结果的顺序吗?

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

推荐答案

Tasks.whenAllSuccess(以及其他whenAll方法)会将任务中的结果按传递给whenAllSuccess的顺序传递给List中的回调.如果您需要其他获取结果的方法,则可以使用原始Task对象维护某种数据结构,然后根据需要查询它们.

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.

这篇关于Tasks.whenAllSuccess是否保证我将任务传递给它的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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