java RxJava - Observable 的 zip 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43524709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RxJava - zip list of Observable
提问by asf
I have a list of Observables (RxJava 1).
我有一个 Observable 列表(RxJava 1)。
List<Observable> observableList = new ArrayList<>();
It can contain at least 1 Observable. Each has the same type of the result.
它可以包含至少 1 个 Observable。每个都有相同类型的结果。
How can I zip results of the all Observables?
如何压缩所有 Observables 的结果?
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-operator 但它不支持 List 并且我不知道可观察的数量(它可以是 1,2,3,4 ....)
回答by et_l
You can use the static zip(java.lang.Iterable<? extends Observable<?>> ws,FuncN<? extends R> zipFunction)
method.
您可以使用静态zip(java.lang.Iterable<? extends Observable<?>> ws,FuncN<? extends R> zipFunction)
方法。
It is a zip
method that takes an Iterable
of Observable
s and a FuncN
(which takes a varargsparameter 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
.
它是一个zip
方法,它接受一个Iterable
of Observable
s 和一个FuncN
(它的方法接受一个可变参数参数call
)并使用它来将相应的发出的Object
s 组合到结果中,以被新的返回的Observable
.
So for example you could do:
例如,你可以这样做:
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;
}
});
回答by Sharan
ReactiveX - Zip operator
ReactiveX - Zip 运算符
Zip beyond BiFunction
Zip 超越 BiFunction
Zip combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function
Zip 通过指定的函数将多个 Observable 的发射组合在一起,并根据该函数的结果为每个组合发射单个项目
Here, list is an Array List of Observables of whichever type you want to pass.
在这里,list 是您要传递的任何类型的 Observable 的数组列表。
val list = arrayListOf<Observable<ImageUrlResponse>>()
Observable.zip(list) { args -> Arrays.asList(args) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
val response = it[0]
val imageUrlResponse = imageUrlObject as ImageUrlResponse
urls.add(imageUrlResponse.imageUrl)}
}, {
val c = it
})
The Result of the following subscription is this image below. Just like we expect it to be zipped together. Also can you notice it returns all the responses to be zipped in a single java.lang.Object[].
以下订阅的结果是下面的这张图片。就像我们期望它被压缩在一起一样。您是否还注意到它返回要压缩到单个java.lang.Object[]中的所有响应。
Note I had to type cast my Array List to access my single object because it is of type Any!
注意我必须键入我的数组列表才能访问我的单个对象,因为它是 Any 类型的!
回答by Pim
I struggled with this as well, and used Sharan's solution as a base for mine.
我也为此苦苦挣扎,并使用 Sharan 的解决方案作为我的基础。
My use case was doing API calls to several 3rd party providers, and then putting each individual result in a List. Each item in the list contains what the API returned, be it success or failure.
我的用例是对几个 3rd 方提供者进行 API 调用,然后将每个单独的结果放在一个列表中。列表中的每一项都包含 API 返回的内容,无论是成功还是失败。
In the end it actually looks quite elegant. In my specific case "ResultType" was replaced with something like "ApiGenericResponseObject".
最后它实际上看起来很优雅。在我的特定情况下,“ResultType”被替换为“ApiGenericResponseObject”之类的东西。
Observable.zip(listOfObservables, args -> {
List<ResultType> result = new ArrayList<>();
for (Object o: args) {
ResultType c = (ResultType) o;
// additional code here, I am just concatenating them together
// This gives me a list with the individual result of each Observable (for instance an API call)
result.add(c);
}
return result;
});
Alternatively, as a Lambda it looks neater. Though I wonder whether someone reading this will understand what is going on:
或者,作为 Lambda,它看起来更整洁。虽然我想知道阅读本文的人是否会理解发生了什么:
Observable.zip(listOfObservables, args -> Arrays.stream(args)
.map(object -> (ResultType) object)
.collect(Collectors.toList())
);
Hope it helps!
希望能帮助到你!