RxJava: Blocking on an observable after subscription?

1k views Asked by At

I have a special need for an observable.

Usually, my observables run in a different thread. But, sometimes they need to block another thread in the middle of subscription. Something the way a future behaves.

An example:

val o = Observable.create(/* computation */)
  .subscribeOn(Schedulers.newThread())
  .observeOn(/* current thread */);

val s = o.subscribe(/* subscriber */);`

Suddenly, an event happens on yet another thread that signals the current thread that it should wait for the execution of the subscription s. (An example would be Android's onPause.)

How do I do that? How do I wait on the subscription s and even possibly retrieve all of the results?

(Subjects?)

1

There are 1 answers

0
stojanman On

The simplest solution for this seems to be:

  1. Make the observable be a caching one with Observable#cache(). (If your observable is a stream of values, use an appropriate Subject and use that as the observable.)
  2. Subscribe to the cached observable / subject.
  3. When the event occurs, simply subscribe to the cached observable / subject on the thread(s) you require. Cancel the subscription as appropriate.