RxJava2 在 Single 上通过 flatMap 传递数据
RxJava2 passing data through flatMap on a Single
在 rxjava 1 中 Observable 有这个平面图方法
public final Observable flatMap(Func1 collectionSelector, Func2 resultSelector)
这使您可以 pass/combine 将初始结果发送给平面图订阅者。
如何使用 RxJava2 获得相同的结果?
我有一个发出A
的Single,我需要根据A
得到B
,然后同时使用A
和B
来执行一个动作。
你在 RxJava2 上有相同的方法,都在 Observable and Flowable ,
但是,在 RxJava1 和 2 中,都没有 Single
这样的运算符,你可以转换 Single
to Observable
然后应用这个运算符。
你试过 CombineLatest (http://reactivex.io/documentation/operators/combinelatest.html)
基本上你可以发射 A 和 B,然后 return 另一个基于函数结果的对象:
RXJava1
Observable
.combineLatest([Add here your A observable],
[Add here your B observable],
new Func2<A, B, Result>() {
@Override
public Result call(A a, B b) {
//Do your stuff here combining both results and return the result expected
}
})
RXJava2
Observable
.combineLatest([Add here your A observable],
[Add here your B observable],
new BiFunction<A, B, Result>() {
@Override
public Result apply(A a, B b) throws Exception {
//Do your stuff here combining both results and return the result expected
}
})
Yosriz的回答是正确的,但添加一个代码示例:
假设如下:
class A {}
class B {}
class AB {
private final A a;
private final B b;
AB(A a, B b) {
this.a = a;
this.b = b;
}
}
interface AbRepository {
Single<A> getA();
Single<B> getB(A a);
}
请注意,方法 getB
需要一个 A
作为参数。
那么你可以这样做:
abRepository.getA()
.toObservable()
.flatMap(new Function<A, ObservableSource<B>>() {
@Override
public ObservableSource<B> apply(A a) throws Exception {
return abRepository.getB(a)
.toObservable();
}
}, new BiFunction<A, B, AB>() {
@Override
public AB apply(A a, B b) throws Exception {
return new AB(a, b);
}
})
.firstOrError();
在 rxjava 1 中 Observable 有这个平面图方法
public final Observable flatMap(Func1 collectionSelector, Func2 resultSelector)
这使您可以 pass/combine 将初始结果发送给平面图订阅者。
如何使用 RxJava2 获得相同的结果?
我有一个发出A
的Single,我需要根据A
得到B
,然后同时使用A
和B
来执行一个动作。
你在 RxJava2 上有相同的方法,都在 Observable and Flowable ,
但是,在 RxJava1 和 2 中,都没有 Single
这样的运算符,你可以转换 Single
to Observable
然后应用这个运算符。
你试过 CombineLatest (http://reactivex.io/documentation/operators/combinelatest.html)
基本上你可以发射 A 和 B,然后 return 另一个基于函数结果的对象:
RXJava1
Observable
.combineLatest([Add here your A observable],
[Add here your B observable],
new Func2<A, B, Result>() {
@Override
public Result call(A a, B b) {
//Do your stuff here combining both results and return the result expected
}
})
RXJava2
Observable
.combineLatest([Add here your A observable],
[Add here your B observable],
new BiFunction<A, B, Result>() {
@Override
public Result apply(A a, B b) throws Exception {
//Do your stuff here combining both results and return the result expected
}
})
Yosriz的回答是正确的,但添加一个代码示例:
假设如下:
class A {}
class B {}
class AB {
private final A a;
private final B b;
AB(A a, B b) {
this.a = a;
this.b = b;
}
}
interface AbRepository {
Single<A> getA();
Single<B> getB(A a);
}
请注意,方法 getB
需要一个 A
作为参数。
那么你可以这样做:
abRepository.getA()
.toObservable()
.flatMap(new Function<A, ObservableSource<B>>() {
@Override
public ObservableSource<B> apply(A a) throws Exception {
return abRepository.getB(a)
.toObservable();
}
}, new BiFunction<A, B, AB>() {
@Override
public AB apply(A a, B b) throws Exception {
return new AB(a, b);
}
})
.firstOrError();