I have two Observables
.
Observable<A> getAObservable()
Returns Observable of A
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 in B
. Now at this point I am not having access to A
data.
Hence I can't return Observable
- I can use
zip
so that once I get the result of both Observable<A>
and Observable<B>
, I can combine their result to
AB
. But here I cannot set dependency that Observable<A>
should
execute before Observable<B>
and I can't pass the result of
Observable<A>
to Observable<B>
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 Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…