每分钟重复一次可观察的最佳方法 rxjava [英] Best way to repeat an observable every minute rxjava

查看:77
本文介绍了每分钟重复一次可观察的最佳方法 rxjava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法:

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

没有更好的方法吗?我正在使用 RxJava2.另外,上面的方法只调用了 10000 次.我想永远调用它,就像使用 Handler.postDelayed() 一样.

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

推荐答案

事实证明这是有效的:

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

这篇关于每分钟重复一次可观察的最佳方法 rxjava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆