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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With