J. K. J. K. - 4 months ago 14
Android Question

How do I convert this RxJava observable to emit results every 200 ms?

I have this Observable:

Observable<String> concatenatedSets =
Observable.just("1/5/8", "1/9/11/58/16/", "9/15/56/49/21");

concatenatedSets.flatMap(s -> Observable.from(s.split("/")))
.map(s -> Integer.valueOf(s))
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(i -> tvCounter.setText(String.valueOf(i)));


how do I convert it or what operator do I chain up to it so that
onNext
is called with 200 ms in between calls?

I was looking at this SO question:
Pause between call onNext in RxJava

but answers there start with creating a pause using
interval
like so

Observable.interval(100, TimeUnit.MILLISECONDS)


However, I am creating my observable in another way (using
just
), so how do I mix both functionalities (
just
and
interval
) or what do I do to achieve a pause of 200 ms between emissions?

PS: just tried
delay
- it postponed overall execution once by the time I provided.
zip
might be the answer but it is very confusing, so how do I zip my observable and an interval one?

Answer
Observable<Integer> observable = Observable.just("1/5/8", "1/9/11/58/16/", "9/15/56/49/21")
                .flatMap(s -> Observable.from(s.split("/")))
                .map(s -> Integer.valueOf(s));

Observable<Integer> zippedObservable = Observable.zip(
                observable,
                Observable.interval(100, TimeUnit.MILLISECONDS),
                (number, interval) -> number);

zippedObservable
        .subscribeOn(Schedulers.computation())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(i -> tvCounter.setText(String.valueOf(i)));