Martynas Jurkus
Martynas Jurkus

Reputation: 9301

Repeat observable unless interrupted

How can I create an Observable that will be repeated n times unless some condition satisfied and then it should be interrupted?

Looking something like repeatUnless or similar operator.

Upvotes: 2

Views: 3383

Answers (1)

akarnokd
akarnokd

Reputation: 69997

There are two options:

1) If you want to interrupt the value sequence:

source.repeat(n).takeWhile(condition);
source.repeat(n).takeUntil(condition);

2) If you want to repeat n times or until a condition is no longer satisfied:

source.repeatWhen(o -> o.scan(1, (a, b) -> a + 1).takeUntil(i -> i < n || condition)))

Upvotes: 6

Related Questions