Reputation: 225
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?
Upvotes: 7
Views: 3430
Reputation: 11515
Rxjava has a doOnNext operator which is what you're looking for.
Upvotes: 4