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