RxJava - Observable的zip列表 [英] RxJava - zip list of Observable
问题描述
我有一个Observables列表(RxJava 1)。
I have a list of Observables (RxJava 1).
List<Observable> observableList = new ArrayList<>();
它可以包含至少1个Observable。每个都有相同类型的结果。
It can contain at least 1 Observable. Each has the same type of the result.
如何压缩所有Observables的结果?
How can I zip results of the all Observables?
I想到了zip-operator,但它不支持List,我不知道observables的数量(可以是1,2,3,4 ....)
I thought about zip-operator but it doesn't support List and I don't know quantity of observables (it can be 1,2,3,4....)
推荐答案
你可以使用静态 zip(java.lang.Iterable<?extends Observable<?>> ws,FuncN<?extends R> zipFunction)
方法。
You can use the static zip(java.lang.Iterable<? extends Observable<?>> ws,FuncN<? extends R> zipFunction)
method.
这是 zip
方法,需要可重复的
可观察的
和一个 FuncN
(需要 varargs其参数调用
方法)并使用它来组合相应的发射对象
si否则新返回的 Observable
将省略结果。
It is a zip
method that takes an Iterable
of Observable
s and a FuncN
(which takes a varargs parameter for its call
method) and uses it to combine the corresponding emitted Object
s into the result to be omitted by the new returned Observable
.
例如你可以这样做:
Observable.zip(observableList, new FuncN(){
public ReturnType call(java.lang.Object... args){
ReturnType result; //to be made
//preparatory code for using the args
for (Object obj : args){
ReturnType retObj = (ReturnType)obj;
//code to use the arg once at a time to combine N of them into one.
}
return result;
}
});
这篇关于RxJava - Observable的zip列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!