I am a newer on RXJava/RXAndroid. I want to implement this case: chose different way based on some condition in RXJava. For example, first, I fetch user info from network and if this is a VIP user, I will go on to fetch more info from network or just show some info in main thread (break the chain.) Here the flow chart: https://i.stack.imgur.com/0hztR.png
I do some search on this and only find "switchIfEmpty" may help. I write the following code:
getUserFromNetwork("userId")
.flatMap(new Function<User, ObservableSource<User>>() {
@Override
public ObservableSource<User> apply(User user) throws Exception {
if(!user.isVip){
//show user info on MainThread!
return Observable.empty();
}else{
return getVipUserFromNetwork("userId");
}
}
}).switchIfEmpty(new ObservableSource<User>() {
@Override
public void subscribe(Observer<? super User> observer) {
//show user info in main thread
//just break the chain for normal user
observer.onComplete();
}
}).doOnNext(new Consumer<User>() {
@Override
public void accept(User user) throws Exception {
//show vip user info in main thread
}
}).subscribe();
Is there more simple way to achieve this?
Thanks!