简体   繁体   中英

Call void method and pass trought arguments RxJava

I'm looking for a better to do this :

mRestService.login(email, password) // Login user in
                .flatMap(user -> Observable.zip(
                        mRestService.start(user._token), // Start his session
                        Observable.just(user),
                        (v, u) -> (User) u // Pass user throught
                ))
                .subscribe(user -> {
                }, throwable -> {                     
                });

But I coudn't come up with something better.

the start method on the mRestService perform a side effect. So you can use doOnNext method which is here for this sort of side effect.

 mRerstService.login(email, password)
              .doOnNext(u -> mRestService.start(user._token))
              .subscribe(); 

You should call mRestService.start(user._token) in flatMap. The user object will be passed to subscribe where you can use it.

 mRestService.login(email, password) // Login user in
                .flatMap((user -> mRestService.start(user._token)))
                .subscribe(user -> {
                }, throwable -> {
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM