How to use if-else in Java RX chain?

I am new to RXJava / RXAndroid. I want to implement this case: I chose another method based on some specific state in RXJava. For example, firstly, I get information about a user from the network, and if this is a VIP user, I'm going to get additional information from the network or just show some information in the main stream (break the chain). Here's the flowchart: https://i.stack.imgur.com/0hztR.png

I am doing a search on this and can find "switchIfEmpty". I am writing 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 an easier way to achieve this?

Thank!

+4
source share
2

flatMap() - , , ( ). switchIfEmpty() , , Observable.empty() ( onCompleted()), , , , , , , , , .

, ( ) , , , VIP , . :

getUserFromNetwork("userId")
            .flatMap(new Function<User, ObservableSource<User>>() {
                @Override
                public ObservableSource<User> apply(User user) throws Exception {
                    if (!user.isVip) {
                        return Observable.just(user);
                    } else {
                        return getVipUserFromNetwork("userId");
                    }
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(user -> {
                if (user.isVip){
                    //display vip user
                }else{
                    //display regular user
                }
            });

, " " .

, ( ), 2 -, getUserFromNetwork() Observable Observable 2 Observable, , , getVipUserFromNetwork(), - , . ( )

+9

switchIfEmpty, . Rx , . . @yosriz, switchIfEmpty onComplete .

, switchIfEmpty, , - .

:

  • ,
  • .

.

, , , switchIfEmpty .

"" "VIP-" /. Java 8 Lambdas , , IF.

  // User Observable, cached so only 1 network call is done
Observable<User> user = getUserFromNetwork("USER_ID").cache();
  // This observable is the user VIP Status as a boolean stream
Observable<Boolean> isVip = user.map(u -> u.isVip() );

, isVip , VIP, VIP, flatMap .

Observable<User> vipUser = isVip
    // If VIP emit downstream
    .filter(vip -> vip)
    // This flatmap is ignored if 
    // the emission is filtered out ( vip -> vip == false )
    .flatMap(vip -> user.flatMap(usr -> {
        return getVipUserFromNetwork(usr.getId());
    }));
});

vipUser

  • , .

, switchIfEmpty

vipUser.switchIfEmpty(user)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(usr -> {
        // Logging the class just to understand the outcome
        System.out.println("User instanceOf " + usr.getClass());
    });

Observable<User> user = getUserFromNetwork("USER_ID").cache();
Observable<Boolean> isVip = user.map(u -> u.isVip() );

Observable<User> vipUser = isVip
    .filter(vip -> vip)
    .flatMap(vip -> user.flatMap(usr -> {
        return getVipUserFromNetwork(usr.getId());
    }));
});

vipUser.switchIfEmpty(user)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(usr -> {
        // Handle UI Changes
    });
0

Source: https://habr.com/ru/post/1674706/


All Articles