java RxJava - 每秒发出一个 observable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35688619/
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-11-03 00:27:42  来源:igfitidea点击:

RxJava - emit an observable every second

javaandroidsystem.reactiverx-javakotlin

提问by fergdev

I am making a timer in Android using RxJava. I need to make a timer in RxJava to emit an observable every second. I have tried the following but with no luck. Any thoughts on what I am doing wrong?

我正在使用 RxJava 在 Android 中制作计时器。我需要在 RxJava 中创建一个计时器,以每秒发出一个可观察对象。我尝试了以下但没有运气。关于我做错了什么的任何想法?

Observable.interval(1000L, TimeUnit.MILLISECONDS)
          .timeInterval()
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe({Log.d(LOG_TAG, "&&&& on timer") })

回答by hotkey

Your code seems not to be called. Check whether it is executed and when. As of working with Observable, it is completely correct.

您的代码似乎没有被调用。检查它是否被执行以及何时执行。在使用时Observable,它是完全正确的。

For example, I put your snippet inside onCreate(...)of my MainActivity:

例如,我将您的代码段放在onCreate(...)my 中MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Observable.interval(1000L, TimeUnit.MILLISECONDS)
            .timeInterval()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { Log.d("tag", "&&&& on timer") }
    // ...
}

And it works:

它有效:

https://www.dropbox.com/s/jxkm5ol8l5idyji/observable_interval.png?dl=0

https://www.dropbox.com/s/jxkm5ol8l5idyji/observable_interval.png?dl=0

Also, probably you don't need .timeInterval()because Observable.interval(...)itself emits sequential numbers within the specified rate, and .timeInterval()just transforms it to emit the time intervals elapsed between the emissions.

此外,您可能不需要,.timeInterval()因为Observable.interval(...)它本身会在指定的速率内发出序列号,而.timeInterval()只是将其转换为发出两次发射之间经过的时间间隔。

回答by Vesko

In your subscribe()you don't consume the longTimeIntervalobject that's returned by the timeInterval()operator.

在您中,subscribe()您不使用运算符longTimeInterval返回的对象timeInterval()

Correct version:

正确版本:

.subscribe(longTimeInterval -> {
     Log.d(LOG_TAG, "&&&& on timer"); 
}

Also I think you don't need the timeInterval()operator at all. Observable.interval()will emit an observable every second in your case, which I guess is what you want. timeInterval()transforms that to an observable that holds the exact time difference between two events occur, I doubt you'll need that.

另外我认为您根本不需要timeInterval()操作员。Observable.interval()在您的情况下,每秒都会发出一个 observable,我想这就是您想要的。timeInterval()将其转换为可观察到的两个事件发生之间的确切时间差,我怀疑您会需要它。

回答by kosiara - Bartosz Kosarzycki

In Kotlin & RxJava 2.0.2 simply define an Observable Interval with an initialDelay0 and period1 which will emit list item each second.

在 Kotlin & RxJava 2.0.2 中,只需定义一个带有initialDelay0 和period1的 Observable Interval ,它将每秒发出列表项。

val list = IntRange(0, 9).toList()
val disposable = Observable
    .interval(0,1, TimeUnit.SECONDS)
    .map { i -> list[i.toInt()] }
    .take(list.size.toLong())
    .subscribe {
       println("item: $it")
    }
Thread.sleep(11000)