简体   繁体   English

启动RxJava2 Observable.interval()时没有初始延迟吗?

[英]Start RxJava2 Observable.interval() with no initial delay?

I'm using a RxJava2 Observable to repeat an operation every second and later post the results to a TextView in my Android layout. 我正在使用RxJava2 Observable每秒重复一次操作,然后将结果发布到Android布局中的TextView中。 The code I have right now (below) is working great, but I would rather have it execute with no initial delay. 我现在(下面)的代码运行良好,但是我希望它没有初始延迟地执行。

Disposable disposable = Observable.interval(1000, java.util.concurrent.TimeUnit.MILLISECONDS)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(aLong -> myMethod());

I've noticed that others using RxJS have used timer() instead of interval() to achieve 0 delay. 我注意到其他使用RxJS的人使用timer()而不是interval()来实现0延迟。 Is there any way to do this with RxJava2? 有什么办法可以用RxJava2做到这一点?

There is a Observable.timer(long delay, TimeUnit unit, Scheduler scheduler) in RxJava 2 . RxJava 2中有一个 Observable.timer(长延迟,TimeUnit单位,Scheduler调度程序) You can try using that. 您可以尝试使用。

Update 1: 更新1:
For an interval that starts with an initial delay and then emits on a regular interval, you can use Observable.interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) 对于一个时间间隔与一个初始延迟启动,然后发出对有规律的间隔,可以使用Observable.interval(长在initialDelay,周期长,TIMEUNIT单元,调度器调度器)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM