johnny_crq
johnny_crq

Reputation: 4391

Java RxJava Emit Items From List Infinitly

I want emit items with a specified delay, repeatedly from an arraylist.

    Observable.from(saverFileNameList)
            .subscribeOn(AndroidSchedulers.mainThread())
            .flatMap(urlString -> Observable.just(urlString)
                            .delay(5000, TimeUnit.MILLISECONDS)
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnNext(urlString1 -> {
                                ...


                            })
            ).repeat()
            .subscribe();

This is not working, for some reason, i have set a print log on onNext, and i can see everything is being printed very fast so the delay is not being applied. What am i doing wrong

Upvotes: 0

Views: 69

Answers (1)

akarnokd
akarnokd

Reputation: 70017

If you want to delay the emission of the source by the given amount, you can use the following:

Observable.from(saverFileNameList)
.delay(5000, TimeUnit.MILLISECONDS)
.repeat()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(v -> { }, Throwable::printStackTrace);

If you want to have in-between delay, use the following:

Observable.from(saverFileNameList)
.zipWith(Observable.interval(5, SECONDS).onBackpressureBuffer(), (a, b) -> a)
.repeat()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(v -> { }, Throwable::printStackTrace);

Upvotes: 2

Related Questions