RXJava how to try to get the following after x time

I want to make a web service call using a modification every x seconds until condition y is raised.

I want to run OrderApi.get in x seconds until the answer is zero.

 public class OrderApi {} public static Observable<Order> get() { //... } } OrderApi.get(order.getId())) .subscribe(updatedOrder -> { mShouldRun = updatedOrder != null; }); 

Already seen operators like Observable.delay Observable.timber , but I can not find a way to use them correctly.

+5
source share
2 answers

it 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); 
+4
source

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()); } }); 

callback call is called every 1 sec

+1
source

Source: https://habr.com/ru/post/1234311/


All Articles