RxJava, execute code in the observer thread before chaining two observables

I'm using RxJava and RxAndroid, and I want to combine two observables but in between I need to update the UI, so I must execute code in the main thread before reaching the subscriber.

One solution, instead of flatmapping (is that an accepted term?) two observables, would be to call the next observable in the subscriber just after updating the UI, but I feel that there should be a more elegant solution like:

myObservable
    .map(new Func1<Object, Object>() {
        @Override
        public Object call(Object object) {
            /* do stuff on the main thread */
            return object;
        }
    })
    .flatMap(new Func1<Object, Observable<OtherObject>>() {
        @Override
        public Observable<OtherObject> call(Object o) {
            return new MyOtherObservable(o);
        }
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread());

Of course, probably map is not the operator I need to use here. So, is there an operator or a better way to achieve this? Or am I missing the point about how observables should work?


Rxjava有一个doOnNext操作符,它就是你正在寻找的。

链接地址: http://www.djcxy.com/p/84544.html

上一篇: 如何在Java中将字符串转换为日期,无论系统格式是什么

下一篇: RxJava,在链接两个可观察对象之前在观察者线程中执行代码