RxJava, How do I update views in different states of Observables call chains?

I need to do one task with one Observable, get the emitted data, and then use another Observable to make another request. I usually used flatmap

to link two observables and just take care of the last piece of data emitted. Now the view needs to be refreshed during the process. Let's say when the first Observable function emits data, I have to show a text string in the UI.

I know I can split it in half by naming the second observable inside the onNext()

first, but this is cumbersome.

How can I achieve what I described above without going down this path? Thanks to

+3


source to share


1 answer


Here's an example from an old project with doOnNext () :

API.getVideoListObservable()
                .doOnError(t -> t.printStackTrace())
                .map(r -> r.getObjects())
                .doOnNext(l -> VideoActivity.this.runOnUiThread(() -> fragment.updateVideoList(l)))
                .doOnNext(l -> kalturaVideoList.addAll(l))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();

      

Pay attention to .doOnNext(l -> VideoActivity.this.runOnUiThread(() -> fragment.updateVideoList(l)))

- then you can use flatmap()

at any time, and doOnNext()

will run untilflatmap()



Good explanation doOnNext()

by Simon Baslé here :

doOnNext

for side effects: you want to react (like a log) to an outlier at an intermediate stage in your flow, for example before the flow is filtered, for lateral behavior like logging, but you still want the value to propagate downstream.

onNext

is more final, it consumes value.

+2


source







All Articles