RxJava delay for each item of list emitted

后端 未结 15 1821
感情败类
感情败类 2020-11-29 00:14

I\'m struggling to implement something I assumed would be fairly simple in Rx.

I have a list of items, and I want to have each item emitted with a delay.

It

15条回答
  •  被撕碎了的回忆
    2020-11-29 00:54

    For kotlin users, I wrote an extension function for the 'zip with interval' approach

    import io.reactivex.Observable
    import io.reactivex.functions.BiFunction
    import java.util.concurrent.TimeUnit
    
    fun  Observable.delayEach(interval: Long, timeUnit: TimeUnit): Observable =
        Observable.zip(
            this, 
            Observable.interval(interval, timeUnit), 
            BiFunction { item, _ -> item }
        )
    

    It works the same way, but this makes it reusable. Example:

    Observable.range(1, 5)
        .delayEach(1, TimeUnit.SECONDS)
    

提交回复
热议问题