I have the following Hot Observable:
hotObservable = Observable.interval(0L, 1L, TimeUnit.SECONDS)
.map((t) -> getCurrentTimeInMillis()))
However, I can't find a good way to stop it. I was able to partially solve this using takeWhile
and a boolean
flag (runTimer
):
Observable.interval(0L, 1L, TimeUnit.SECONDS)
.takeWhile((t) -> runTimer)
.map((t) -> getCurrentTimeInMillis()))
There are 2 things I don't like in this approach though:
- I must keep the flag
runTimer
around, which I don't want. - Once
runTimer
becomesfalse
, the Observable simply completes, which means if I want to emit again I need to create a new Observable. I don't want that. I just want the Observable stop emitting items until I tell it to start again.
I was hoping for something like this:
hotObservable.stop();
hotObservable.resume();
That way I don't need to keep any flags around and the observable is always alive (it might not be emitting events though).
How can I achieve this?
takeWhile
operator in your previous solution withfilter
operator, you would get rid of the second disadvantage... – Guayule