Java Android RX - Observable.timer 只触发一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32524125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:42:11  来源:igfitidea点击:

Android RX - Observable.timer only firing once

javaandroidrx-android

提问by James King

So I am trying to create an observable which fires on a regular basis, but for some reason which I cannot figure out, it only fires once. Can anyone see what I am doing wrong?

所以我试图创建一个定期触发的 observable,但由于某种我无法弄清楚的原因,它只触发一次。谁能看到我做错了什么?

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

currently delay is set to 2

当前延迟设置为 2

采纳答案by Bryan Herbst

The documentation for the timer operatorsays this:

计时器操作员的文档是这样说的:

Create an Observable that emits a particular item after a given delay

创建一个在给定延迟后发出特定项目的 Observable

Thus the behavior you are observing is expected- timer()emits just a single item after a delay.

因此,您正在观察的行为是预期的 -timer()在延迟后仅发出一个项目。

The intervaloperator, on the other hand, will emit items spaced out with a given interval.

另一方面,间隔运算符将发出以给定间隔间隔开的项目。

For example, this Observable will emit an item every second:

例如,这个 Observable 将每秒发出一个项目:

Observable.interval(1, TimeUnit.SECONDS);

回答by Hyman The Ripper

I know topic is old but maybe for future visitors. (5 min count down timer)

我知道话题很老,但也许是为了未来的访客。(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
            });

回答by 1'hafs

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