lujop
lujop

Reputation: 13873

Subscribe subscriber to another observable?

Is there any problem in subscribe an observable's subscriber to another observable?

That is something like:

Observable.create((Subscriber<? super Response> subscriber) -> {
        Observable<T1> o1 = createObservableT1(location); //Hot observable
        Observable<T2> o2 =  createObservableT2(location); //Hot observable

        Observable.zip(o1,o2,(T1 r1, T2 r2) -> {
            return createResponse(r1,r2);
        }).subscribe(subscriber);  //Subscribe our subscriber to the created observable                    
    });

Is it a good pattern or there are some hidden drawbacks? If there is some drawbacks how can I do it taking in account that o1, and o2 are hot observables that need to be combined to create the returned Observable that has to be mantained as a cold one.

Upvotes: 0

Views: 107

Answers (1)

zsxwing
zsxwing

Reputation: 20826

I cannot find any problem. But I would recommend defer, such as

Observable.defer(
    () -> createObservableT1(location)
                  .zipWith(createObservableT2(location),
                      (r1, r2) -> createResponse(r1, r2)));

Upvotes: 1

Related Questions