[英]Start RxJava2 Observable.interval() with no initial delay?
[英]Android RxJava Observable.interval() doesn't stop emitting items
我有一個靜態字段
private static Subscription timer;
和兩個靜態方法:
public static void setTimer() {
timer = Observable.interval(2, TimeUnit.SECONDS, Schedulers.computation())
.doOnNext(tick -> update(tick))
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
public static void removeTimer() {
if (timer != null && !timer.isUnsubscribed()) {
timer.unsubscribe();
timer = null;
}
}
在取消補給后猜測Observable必須停止發射物品。 但它不起作用。 如果函數updatePrices是
private static void update(long tick) {
Log.d(TAG, "tick");
}
調用removeTimer()后,將繼續打印日志。
那么問題是如何正確地停止在我的觀察中發射物品?
問題是雙重調用setTimer()。
不過我還有一個問題。 任何人都可以解釋為什么在第二次調用setTimer()之后,舊的計時器副本仍會繼續發出項目?
也許為時已晚,但可能有助於其他人!
Observable.interval(TICK_MILLIS, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.map(v -> v * TICK_MILLIS) // use to map value to onNext func,
.takeUntil(v -> v > MAX_TICKS) // Stop Timer here.
.take() // You can use take to stop timer after N count
.subscribe(this::onNext, Log::d ,this::onComplete);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.