Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2: Repeat conditonally / don't repeat in `repeatWhen`

I have a Observable I want to repeat periodically, but only under a condition:

apiInterface.getData() // returns Observable<Data>
... // processing is happening here
.toList()
.repeatWhen(completed -> {
    if (autoReload){
        // Repeat every 3 seconds
        return completed.delay(3, TimeUnit.SECONDS);
    } else {
        return ??? // What do I have to return that it does not repeat?
    }
})
.subscribe(list -> callbackInterface.success(list));

My question is: What do I have to return in the else statement to not repeat the Observable (just execute the chain once)?

like image 564
Thomas Avatar asked Jun 28 '17 09:06

Thomas


1 Answers

You have to react to the completion indicator by something that signals completion in response to an item, for example:

completed.takeWhile(v -> false);

Unfortunately, empty() doesn't work there because it immediately completes the sequence before the source could even run.

like image 61
akarnokd Avatar answered Nov 18 '22 12:11

akarnokd