Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

java - RxJava/Android: Combine result of two dependent Observables

I have two Observables.

  1. Observable<A> getAObservable() Returns Observable of A
  2. 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:

  1. 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

    1. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See flatMap documentation. It has overloaded method, that apply zip function:

    Observable<Pair<A, B>> abObservable = getAObservable()
      .flatMap(
        new Func1<A, Observable<? extends B>>() {
            @Override
            public Observable<? extends B> call(A a) {
                return getBObservable(a);
            }
        },
        new Func2<A, B, Pair<A, B>>() {
            @Override
            public Pair<A, B> call(A a1, B b1) {
                return new Pair<>(a1, b1);
            }
        }
      );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...