angel
angel

Reputation: 77

RxJava Observable.Interval end emitting

I'd like to have repeating observable that emits items until some condition comes true. Then it calls onComplete and ends emitting.

I though that something like this would work, but I'm wrong:

return Observable.interval(5, TimeUnit.SECONDS)
  .flatMap(tick -> {
     if (condition) {
       return Observable.empty();
     }
     doSomething();
     return Observable.just(null);
   });

Upvotes: 0

Views: 1106

Answers (1)

dwursteisen
dwursteisen

Reputation: 11515

You can use takeUntil :

  return Observable.interval(5, SECONDS)
                   .takeUntil(conditionObs)
                   .subscribe(t -> dosomething());

Please note that conditionObs (which is another observable) will have to emit something when the condition turn to be true.

you can check the doc here : http://reactivex.io/documentation/operators/takeuntil.html

Upvotes: 3

Related Questions