konmik/nucleus

Wondering about multiple Presenter endpoint calls (Question)

fredagsfys opened this issue · 2 comments

I've got two calls in Presenter, each wrapped in a restartableLatestCache. First one is a authentication against a webservice using restrofit, the second is fetching profile data using a token which is retrived from the authentication.

Basically i start the REQUEST_LOGIN on btnClick and on success I kick off the REQUEST_ACCOUNT onAuthenticateSuccess. The issue I'm having is that REQUEST_ACCOUNT starts off before onAuthenticate is done...

I'm overall wondering if this is the way to go in the Presenter if you have multiple endpoint calls?

This is what my Presenter looks like

Presenter

@Override
protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    restartableLatestCache(REQUEST_LOGIN,
            () -> mRetrofit.create(AccountService.class)
                .authenticate("password", "inspection-api", "secret", mEmail, mPassword)
                .subscribeOn(Schedulers.io())
                .observeOn(mainThread()),
            (loginActivity, response) -> loginActivity.onAuthenticateSuccess(response),
            (loginActivity, throwable) -> loginActivity.onAuthenticateError(throwable)
    );

    restartableLatestCache(REQUEST_ACCOUNT,
            () -> mRetrofit.create(AccountService.class)
                    .userinfo("Bearer " + mAccessToken)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(mainThread()),
            (loginActivity, response) -> loginActivity.onAccountInfoSuccess(response),
            (loginActivity, throwable) -> loginActivity.onAccountInfoError(throwable)
    );
}

void request(final String email, final String password) {
    mEmail = email;
    mPassword = password;
    start(REQUEST_LOGIN);
}

void requestAccountInfo(String accessToken){
    mAccessToken = accessToken;
    start(REQUEST_ACCOUNT);
}

Activity

getPresenter().request(email, password);

void onAuthenticateSuccess (AccountToken accountToken) {
    // Request account info
    getPresenter().requestAccountInfo(accountToken.getAccessToken());
}

Hi, I you need to run request sequentially, just join then into one, switchMap or something.

Yes, I forgot to give an answer and close this one. Thanks @konmik !