我有以下方法:
public class ParentalControlInteractor {
public Single<Boolean> isPinSet() {
return bamSdk.getPinManager().isPINSet();
}
}
我想调用此函数运行一次,然后每分钟重复一次,直到无穷大但这看起来很笨拙:
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()。
答案 0 :(得分:4)
试试这个:
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());
});
答案 1 :(得分:3)
你可以使用interval()
oberator这里是代码
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() {
}
});
如果您想在任何时候完成此操作,请致电disposable.dispose()
答案 2 :(得分:2)
事实证明这是在做这项工作:
.text:hover .icon
{
display:inline;
right:0px;
position:absolute;
}
答案 3 :(得分:1)
每次重复发送请求的最佳方式,并具有特定的首次发射延迟
return Observable.interval(FIRST_ITEM_DELAY, CYCLE_TIME, TimeUnit.SECONDS)
.flatMap(aLong -> repository.repeatedRequest());
答案 4 :(得分:0)
您可以组合一些RxJava运算符:
Observable.wrap(parentalControlInteractor.isPinSet().delay(1,TimeUnit.MINUTES)).repeat();
我发现这个解决方案非常优雅且非常简单
答案 5 :(得分:0)
尝试
.repeatWhen(objectFlowable -> Flowable.timer(10, TimeUnit.SECONDS).repeat())