Rezo Shalikashvili
Rezo Shalikashvili

Reputation: 89

RXJava : wait until other observable has completed

I am relatively new to RX world. So, there are many operators which I don't understand. I have spent a few hours to figure out the solution for my problem.

Which is:

I have one 'cold' observable(observable I) which emits only one result. It is for loading some initial data before application could load other items to show

On the other side, I have another 'hot' observable(observable II) which is loading data with paging, therefore emitting many results.

I want observable II to wait for observable I. If at the moment the observable II is created, observable I has already finished, I want observable II to not load initial data again.

To visualise :

  Case 1

  Observable I  [---------------------------------------------------]

  Observable II  .................[----------------------------------------

Here observabele II should wait for observable I and only continue to work after that.

  Case 2 

  Observable I   [--------]

  Observable II ...............[----------------------------------------

Here observable II should only check that observable I is finished and continue working

In both cases, execution of observable I should not happen more than once.

Question:

How can I do this using RX combining operators?

Upvotes: 2

Views: 5837

Answers (1)

RvanHeest
RvanHeest

Reputation: 869

You probably want to use either concat or concatWith here. The operators do the same, it's just static vs non-static methods.

Let's say you got result = obs1.concatWith(obs2); what this operator does is:

  1. it subscribes to obs1 and emits all elements it receives from its onNext.
  2. once obs1 calls onCompleted, it unsubscribes from obs1 and subscribes to obs2. It will likewise emit all elements it receives from obs2's onNext.
  3. once obs2 calls onCompleted, it calls onCompleted as well, since no more elements will come from either Observable.

Upvotes: 3

Related Questions