konmik / nucleus

Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
MIT License
1.98k stars 253 forks source link

Wondering about multiple Presenter endpoint calls (Question) #132

Closed fredagsfys closed 7 years ago

fredagsfys commented 7 years ago

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());
}
konmik commented 7 years ago

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

fredagsfys commented 7 years ago

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