Adham Enaya
Adham Enaya

Reputation: 908

How can I combine two observables based on the result of the first observable?

There are 3 observables, 2 of them can run sequentially, for example

Observable<BaseObject> baseObj = getBaseObs();

Observeable<Object1> obs1 = getObs1();
Observeable<Object2> obs2 = getObs2();

The baseObj should be executed first always, and if the result baseObj.isFirst() equals true, the observable obs1 should be executed immediately, otherwise the obs2 should be executed.

How can I combine two observables based on the result of the first observable?

Upvotes: 0

Views: 429

Answers (1)

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

You can arrange for subsequent observables to be observed using the switchMap() operator:

baseObj
  .switchMap( isFirst -> isFirst ? obs1 : obs2 )
  .subscribe( ... );

The result of the observer chain is the result of either obs1 or obs2, depending on baseObj.

Upvotes: 1

Related Questions