Reputation: 21995
I would like to use RxJava (RxJava2 actually) to periodically send network (HTTP) requests. A first attempt would look more or less like this (adapted from this source):
Observable.interval(5, TimeUnit.SECONDS, Schedulers.io())
.map(tick -> sendNetworkRequest())
This will call sendNetworkRequest every 5 seconds.
However if sendNetworkRequest() takes some time to complete (for example on a slow network, or if there are multiple retries..) the requests would overlap.
What I would like to do is to make sure there is an interval of 5 seconds between requests, i.e. from the end of the previous request to the start of the next one.
How can this be accomplished with RxJava?
Upvotes: 1
Views: 503
Reputation: 3083
Use repeatWhen
Observable.fromCallable(sendNetworkRequest())
.repeatWhen(observable -> observable.delay(5, TimeUnit.SECONDS));
Upvotes: 3