Best way to repeat an observable every minute rxja

2019-06-19 18:24发布

I have the following method:

public class ParentalControlInteractor {
   public Single<Boolean> isPinSet() {
       return bamSdk.getPinManager().isPINSet();
   }
}

I want to call this function to run once, then repeat every minute until infinity but this seems clumsy:

    parentalControlInteractor.isPinSet()
            .subscribeOn(Schedulers.io())
            .repeat(10000)
            .timeout(1600,TimeUnit.MILLISECONDS)
            .doOnError(throwable -> {
                Timber.e(throwable,"Error getting if Pin is set");
                throwable.printStackTrace();
            })
            .subscribe(isPinSet -> {
                this.isPinSet = isPinSet;
                Timber.d("Pin is set = " + isPinSet.toString());
                });

Isn't there a better way to do it? I'm using RxJava2. Also, the method above only calls it 10000 times. I want to call it forever, like using Handler.postDelayed().

6条回答
神经病院院长
2楼-- · 2019-06-19 18:54

Try .repeatWhen(objectFlowable -> Flowable.timer(10, TimeUnit.SECONDS).repeat())

查看更多
我命由我不由天
3楼-- · 2019-06-19 18:56

It turns out this is doing the job:

parentalControlInteractor.isPinSet()
            .subscribeOn(Schedulers.io())
            .delay(10000,TimeUnit.MILLISECONDS)
            .repeat()
            .doOnError(throwable -> {
                Timber.e(throwable,"Error getting if Pin is set");
                throwable.printStackTrace();
            })
            .subscribe(isPinSet -> {
                this.isPinSet = isPinSet;
                Timber.d("Pin is set = " + isPinSet.toString());
                });
查看更多
姐就是有狂的资本
4楼-- · 2019-06-19 18:59

Try this:

parentalControlInteractor.isPinSet()
        .subscribeOn(Schedulers.io())
        .repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
            @Override
            public Observable<?> call(Observable<? extends Void> observable) {
                return observable.delay(60, TimeUnit.SECONDS);
            }
        })
        .doOnError(throwable -> {
            Timber.e(throwable,"Error getting if Pin is set");
            throwable.printStackTrace();
        })
        .subscribe(isPinSet -> {
            this.isPinSet = isPinSet;
            Timber.d("Pin is set = " + isPinSet.toString());
        });
查看更多
【Aperson】
5楼-- · 2019-06-19 18:59

you can use interval() oberator here is the code

DisposableObserver<Boolean> disposable = 
Observable.interval(1, TimeUnit.MINUTES)
            .flatMap(new Function<Long, ObservableSource<? extends Boolean>>() {
              @Override public ObservableSource<? extends Boolean> apply(@NonNull Long aLong)
                  throws Exception {
                return isPinSet().toObservable();
              }
            })
            .subscribeOn(Schedulers.io())
            .subscribeWith(new DisposableObserver<Boolean>() {
              @Override public void onNext(Boolean aBoolean) {

              }

              @Override public void onError(Throwable e) {

              }

              @Override public void onComplete() {

              }
            });

if you want to finish this operation at any time call disposable.dispose()

查看更多
Juvenile、少年°
6楼-- · 2019-06-19 19:04

Best way to repeat request every time with specific delay of first emission

 return Observable.interval(FIRST_ITEM_DELAY, CYCLE_TIME, TimeUnit.SECONDS)
                       .flatMap(aLong -> repository.repeatedRequest());
查看更多
乱世女痞
7楼-- · 2019-06-19 19:09

You can combine some RxJava operators:

Observable.wrap(parentalControlInteractor.isPinSet().delay(1,TimeUnit.MINUTES)).repeat();

I found this solution very elegant and very simple

查看更多
登录 后发表回答