Rxjava: Observable depends on other Observable | Needing both values

Created on 3 Nov 2016  路  2Comments  路  Source: ReactiveX/RxJava

I have an Observable whose emitted item I need for the next Observable but I can't use flatMap() because the method I need to call in the Subscriber needs the result of both Observables.

service.getToken(code)
          .subscribe(new Action1<Token>() {
                        @Override
                        public void call(Token token) {
                            service.getProfile(token.getAccessToken())
                                    .subscribe(
                                    new Action1<Profile>() {
                                        @Override
                                        public void call(Profile profile) {
                                            createAccount(token, profile);
                                        }
                                    });
                        }
                    });

Any better approach then this?

Question

Most helpful comment

Use the flatMap that takes a second Func2:

service.getToken()
.flatMap(token -> service.getProfile(token.getAccessToken()),
    (token, profile) -> { createAccount(token, profile); return true; })
.subscribe(v -> { }, Throwable::printStackTrace)

All 2 comments

Use the flatMap that takes a second Func2:

service.getToken()
.flatMap(token -> service.getProfile(token.getAccessToken()),
    (token, profile) -> { createAccount(token, profile); return true; })
.subscribe(v -> { }, Throwable::printStackTrace)

Thank you for the quick response, that's a far better approach!

Was this page helpful?
0 / 5 - 0 ratings