Android RX - Observable.timer 只发射一次
Android RX - Observable.timer only firing once
所以我正在尝试创建一个定期触发的可观察对象,但由于某种我无法弄清楚的原因,它只触发一次。谁能看出我做错了什么?
Observable<Long> observable = Observable.timer(delay, TimeUnit.SECONDS, Schedulers.io());
subscription = observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
searchByStockHelper.requestRemoteSearchByStock();
}
});
当前延迟设置为 2
the timer operator 的文档是这样说的:
Create an Observable that emits a particular item after a given delay
因此您观察到的行为是预期的 - timer()
在延迟后仅发出一个项目。
另一方面,The interval 运算符将发出以给定间隔间隔开的项目。
例如,这个 Observable 将每秒发出一个项目:
Observable.interval(1, TimeUnit.SECONDS);
我知道这个话题已经过时了,但也许适合未来的访客。 (5 分钟倒计时)
Disposable timerDisposable = Observable.interval(1,TimeUnit.SECONDS, Schedulers.io())
.take(300)
.map(v -> 300 - v)
.subscribe(
onNext -> {
//on every second pass trigger
},
onError -> {
//do on error
},
() -> {
//do on complete
},
onSubscribe -> {
//do once on subscription
});
I implemented like this in my code as it make sure task running is finished before invoking again, and you can update delay.
return Single.timer(5, TimeUnit.SECONDS).flatMap(
new Function<Long, Single<Object>>() {
@Override
public Single<Object> apply(Long aLong) {
//create single with task to be called repeatedly
return Single.create();
}
})
.retry(new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) {
boolean response = true;
//implement your logic here and update response to false to stop
retry
return response;
}
});
所以我正在尝试创建一个定期触发的可观察对象,但由于某种我无法弄清楚的原因,它只触发一次。谁能看出我做错了什么?
Observable<Long> observable = Observable.timer(delay, TimeUnit.SECONDS, Schedulers.io());
subscription = observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
searchByStockHelper.requestRemoteSearchByStock();
}
});
当前延迟设置为 2
the timer operator 的文档是这样说的:
Create an Observable that emits a particular item after a given delay
因此您观察到的行为是预期的 - timer()
在延迟后仅发出一个项目。
The interval 运算符将发出以给定间隔间隔开的项目。
例如,这个 Observable 将每秒发出一个项目:
Observable.interval(1, TimeUnit.SECONDS);
我知道这个话题已经过时了,但也许适合未来的访客。 (5 分钟倒计时)
Disposable timerDisposable = Observable.interval(1,TimeUnit.SECONDS, Schedulers.io())
.take(300)
.map(v -> 300 - v)
.subscribe(
onNext -> {
//on every second pass trigger
},
onError -> {
//do on error
},
() -> {
//do on complete
},
onSubscribe -> {
//do once on subscription
});
I implemented like this in my code as it make sure task running is finished before invoking again, and you can update delay.
return Single.timer(5, TimeUnit.SECONDS).flatMap(
new Function<Long, Single<Object>>() {
@Override
public Single<Object> apply(Long aLong) {
//create single with task to be called repeatedly
return Single.create();
}
})
.retry(new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) {
boolean response = true;
//implement your logic here and update response to false to stop
retry
return response;
}
});