Search code examples
javaandroidrx-javarx-java2

dynamic interval after emitting item in rxjava


I am trying to add dynamic internal for the list items that have been emitted using RxJava.

Currently in my flow after every 10 seconds I am emitting the items but what I am trying to achieve is there will be a delay of 60 seconds after emitting first item then rest of the items will continue following 10 seconds of delay.

  1. item #1 emit
  2. 60 seconds delay
  3. item #1 emit
  4. 10 seconds delay
  5. item #2 emit
  6. 10 seconds delay and so on

My current code is as follows

.flatMap(dataList -> Observable.fromIterable(dataList)
            .zipWith(Observable.interval(10, TimeUnit.SECONDS), (item, interval) -> item)
            .doOnNext(data -> {
                
            })

Solution

  • Use a combination of a just() and an interval() with initial delay of 60

    .zipWith(
            Observable.just(-1L)
            .concatWith(Observable.interval(60, 10, TimeUnit.SECONDS)), 
            (item, interval) -> item
    )