Is it possible to use RxJava/RxAndroid and Retrofit to perform periodic http requests to update data every x seconds?
Currently I am using an IntentService and Recu
Use Observable.interval
and to prevent overlapping requests from service.getAthletes()
subscribe on a single threaded Scheduler
within the flatMap
:
Scheduler scheduler = Schedulers.from(Executors.newSingleThreadExecutor());
Observable.interval(x, TimeUnit.SECONDS)
.flatMap(n ->
service.getAthletes()
.retry(3)
.subscribeOn(scheduler))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Athlete>>() {
@Override
public void call(List<Athlete> athletes) {
// Handle Success
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
// Handle Error
}
});