本文介绍了RxJava中的flatZip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将多个Observable压缩在一起,然后以产生Observable的方式对其进行转换:
I'm zipping multiple Observables together and then transforming them in a way that results in an Observable:
final Observable<Observable<M>> result = Observable.zip(obs1, obs2, transformFunc);
我想做的是:
final Observable<M> result = Observable.flatZip(obs1, obs2, transformFunc);
鉴于flatZip不存在,最干净的方法是什么(也许我应该提交一个).此刻,我不得不将结果平放在自身上.
What's the cleanest way to do this, given flatZip doesn't exist (maybe I should submit one). At the moment I'm having to flatMap the result in on itself.
推荐答案
public class RxHelper {
public static <T1, T2, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, final Func2<? super T1, ? super T2, Observable<? extends R>> zipFunction) {
return Observable.merge(Observable.zip(o1, o2, zipFunction));
}
public static <T1, T2, T3, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Func3<? super T1, ? super T2, ? super T3, Observable<? extends R>> zipFunction) {
return Observable.merge(Observable.zip(o1, o2, o3, zipFunction));
}
public static <T1, T2, T3, T4, R> Observable<R> flatZip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Func4<? super T1, ? super T2, ? super T3, ? super T4, Observable<? extends R>> zipFunction) {
return Observable.merge(Observable.zip(o1, o2, o3, o4, zipFunction));
}
}
这篇关于RxJava中的flatZip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!