I have two Observables
.
Observable<A> getAObservable()
Returns Observable ofA
Observable<B> getBObservable(A)
Returns Observable of 'B'.
Here Observable<A>
should execute before Observable<B>
so that it can pass its result A
to getBObservable()
method. Now once Observable<B>
completes I need to combine the result of these observable
and Observable<AB>
should returns.
Options Tried:
Take
Observable<A>
and apply flatMap on it so that it transform result inB
. Now at this point I am not having access toA
data. Hence I can't return Observable- I can use
zip
so that once I get the result of bothObservable<A>
andObservable<B>
, I can combine their result toAB
. But here I cannot set dependency thatObservable<A>
should execute beforeObservable<B>
and I can't pass the result ofObservable<A>
toObservable<B>
- I can use
Codebase:
public class Data {
A a = new A();
public Observable<AB> getABContext() {
A a = new A();
return getAObservable()
.flatMap(new Func1<A, Observable<AB>>() {
@Override
public Observable<AB> call(final A a) {
return getBObservable(a)
.flatMap(new Func1<B, Observable<AB>>() {
@Override
public Observable<AB> call(B b) {
return Observable.just(new AB(a, b));
}
});
}
});
}
private Observable<A> getAObservable() {
return Observable.just(a);
}
private Observable<B> getBObservable(A a) {
return Observable.just(new B(a));
}
private static class A {
}
private static class B {
B (A a) {
}
}
private static class AB {
AB (A a, B b) {
}
}
}
See
flatMap
documentation. It has overloaded method, that apply zip function: