合并可观察对象列表,然后等待所有完成 [英] Combine a list of Observables and wait until all completed

查看:143
本文介绍了合并可观察对象列表,然后等待所有完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR 如何将Task.whenAll(List<Task>)转换为RxJava?

我现有的代码使用Bolts构建了一系列异步任务,并等待所有这些任务完成之后再执行其他步骤.本质上,它会构建一个List<Task>并返回一个Task,该列表按照

My existing code uses Bolts to build up a list of asynchronous tasks and waits until all of those tasks finish before performing other steps. Essentially, it builds up a List<Task> and returns a single Task which is marked as completed when all tasks in the list complete, as per the example on the Bolts site.

我正在尝试将Bolts替换为RxJava,并且我假设这种方法可以建立一个异步任务列表(大小未知)并将它们包装到一个单独的Observable中,但我不知道如何.

I'm looking to replace Bolts with RxJava and I'm assuming this method of building up a list of async tasks (size not known in advance) and wrapping them all into a single Observable is possible, but I don't know how.

我尝试查看mergezipconcat等...,但由于我似乎正在适应的工作而无法在我要建立的List<Observable>上工作如果我正确地理解了文档,则一次只能访问两个Observables.

I've tried looking at merge, zip, concat etc... but can't get to work on the List<Observable> that I'd be building up as they all seem geared to working on just two Observables at a time if I understand the docs correctly.

我正在尝试学习RxJava,但对它仍然很陌生,因此,如果这是一个明显的问题或在文档中的某个地方得到了解释,请原谅我;我尝试搜寻.任何帮助将不胜感激.

I'm trying to learn RxJava and am still very new to it so forgive me if this is an obvious question or explained in the docs somewhere; I have tried searching. Any help would be much appreciated.

推荐答案

听起来您正在寻找有几种不同的用法,所以让我们看一个例子.假设我们有几种不同类型的简单可观察物:

There are a few different ways of using it, so let's look at an example. Say we have a few simple observables of different types:

Observable<Integer> obs1 = Observable.just(1);
Observable<String> obs2 = Observable.just("Blah");
Observable<Boolean> obs3 = Observable.just(true);

等待它们全部的最简单方法是这样的:

The simplest way to wait for them all is something like this:

Observable.zip(obs1, obs2, obs3, (Integer i, String s, Boolean b) -> i + " " + s + " " + b)
.subscribe(str -> System.out.println(str));

请注意,在zip函数中,参数的具体类型与要压缩的可观察对象的类型相对应.

Note that in the zip function, the parameters have concrete types that correspond to the types of the observables being zipped.

也可以直接压缩可观察对象的列表:

Zipping a list of observables is also possible, either directly:

List<Observable<?>> obsList = Arrays.asList(obs1, obs2, obs3);

Observable.zip(obsList, (i) -> i[0] + " " + i[1] + " " + i[2])
.subscribe(str -> System.out.println(str));

...或通过将列表包装到Observable<Observable<?>>:

...or by wrapping the list into an Observable<Observable<?>>:

Observable<Observable<?>> obsObs = Observable.from(obsList);

Observable.zip(obsObs, (i) -> i[0] + " " + i[1] + " " + i[2])
.subscribe(str -> System.out.println(str));

但是,在这两种情况下,zip函数只能接受单个Object[]参数,因为列表中的可观察对象的类型及其数量是未知的.这意味着zip函数必须检查参数的数量并进行相应的投射.

However, in both of these cases, the zip function can only accept a single Object[] parameter since the types of the observables in the list are not known in advance as well as their number. This means that that the zip function would have to check the number of parameters and cast them accordingly.

无论如何,以上所有示例最终都将打印1 Blah true

Regardless, all of the above examples will eventually print 1 Blah true

编辑:使用Zip时,请确保所有被压缩的Observables都发出相同数量的项目.在以上示例中,所有三个可观察对象都发出了一个项目.如果我们要将其更改为以下内容:

When using Zip, make sure that the Observables being zipped all emit the same number of items. In the above examples all three observables emitted a single item. If we were to change them to something like this:

Observable<Integer> obs1 = Observable.from(new Integer[]{1,2,3}); //Emits three items
Observable<String> obs2 = Observable.from(new String[]{"Blah","Hello"}); //Emits two items
Observable<Boolean> obs3 = Observable.from(new Boolean[]{true,true}); //Emits two items

然后1, Blah, True2, Hello, True是传递到zip函数中的唯一项目.由于其他可观察物均已完成,因此将永远不会压缩3项.

Then 1, Blah, True and 2, Hello, True would be the only items passed into the zip function(s). The item 3would never be zipped since the other observables have completed.

这篇关于合并可观察对象列表,然后等待所有完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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