I wan to do a call to a web service using retrofit every x seconds until y condition is raised.
I want to run OrderApi.get
after x seconds until the response is null.
public class OrderApi {}
public static Observable<Order> get() {
//...
}
}
OrderApi.get(order.getId()))
.subscribe(updatedOrder -> {
mShouldRun = updatedOrder != null;
});
Already saw operators like Observable.delay
Observable.timber
but I cant find a way to use them properly.
this should work
Observable.interval(1, TimeUnit.SECONDS)
.flatMap(new Func1<Long, Observable<?>>() {
@Override
public Observable<?> call(Long aLong) {
return OrderApi.get();
}
}).takeUntil(new Func1<Object, Boolean>() {
@Override
public Boolean call(Object o) {
return o==null;
}
}).subscribe(observer);
a combination of timer
and repeat
should do it
Observable.timer(1000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.repeat(Schedulers.io())
.subscribe(new Action1<Object>() {
@Override
public void call(Object aLong) {
System.out.println(System.currentTimeMillis());
}
});
the callback call
is called every 1 sec
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