Search code examples
rx-java2rx-android

RxJava2 - how to emit hashMap value periodically


I have searched alot on SO and do not think it is duplicate. My problem is straightforward but somehow my solution is not working.

Problem : I have a hashMap and I want to emit value one by one after every 10 seconds.

This is what I have right now which obviously does not yield right value.

Observable.fromIterable(factMap.entries)
        .delay(10, TimeUnit.SECONDS)
        .subscribe { Timber.i("Abhishek $it") }

I tried interval as well but I am not sure how to keep a track of which value has been emitted. As you can see I am not experienced in RxJava, any help would be appreciated.


Solution

  • You can use one of the .zip operators with an observable with interval. For example,

    // RxJava2
    Observable.fromIterable(factMap.entries)
            .zipWith(Observable.interval(10, TimeUnit.SECONDS), BiFunction<...> { t1, t2 -> t1 })
            .subscribe { Timber.i("Abhishek $it") }