Best way to repeat an observable every minute rxjava
Asked Answered
R

6

12

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().

Ratiocinate answered 28/7, 2017 at 14:38 Comment(2)
just call repeat() pass no parameter to it to repeat foreverOops
I don't want it to keep calling this every second, I'd like there to be an interval.Ratiocinate
R
6

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());
                });
Ratiocinate answered 28/7, 2017 at 19:49 Comment(4)
does this run the first time right away or will delay also the first time?Conservative
I believe it delays the first time.Ratiocinate
How to stop the process ?Reinaldoreinaldos
this will just delay the result 10 seconds, it will not repeat, please check my answerAnt
A
16

you can use interval() oberator here is the code

DisposableObserver<Boolean> disposable = 
Observable.interval(1, TimeUnit.MINUTES)
            .flatMap(aLong -> isPinSet().toObservable())
            .subscribeOn(Schedulers.io())
            .subscribeWith({isPinSet -> doSomething()}, {throwable -> handleError()}, {});

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

Ant answered 29/7, 2017 at 12:15 Comment(0)
F
8

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

Fuld answered 20/3, 2019 at 14:4 Comment(0)
K
6

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());
        });
Kotick answered 28/7, 2017 at 15:2 Comment(2)
I'm getting an exception.Ratiocinate
I'm not sure what exception @KristyWelsh is referring to, but this approach seems to work fine.Victoria
R
6

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());
                });
Ratiocinate answered 28/7, 2017 at 19:49 Comment(4)
does this run the first time right away or will delay also the first time?Conservative
I believe it delays the first time.Ratiocinate
How to stop the process ?Reinaldoreinaldos
this will just delay the result 10 seconds, it will not repeat, please check my answerAnt
M
5

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());
Maul answered 12/9, 2018 at 10:6 Comment(0)
C
0

You can combine some RxJava operators:

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

I found this solution very elegant and very simple

Civet answered 1/6, 2018 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.