Sachin Chandil
Sachin Chandil

Reputation: 17809

RxAndroid call one network call after another

I am new to RxJava. I have a scenario where I want to call first login webservice (getLoginObservable) and on success, want to call another webservice (getFetchDataObservable) to get user information.

I have following code working if login is success. But I am unable to figure out how to code failure case.

private void doLogin() {
    emailAddress = editTextUsername.getText().toString();
    final String password = editTextPassword.getText().toString();
    showProgress(null, getString(R.string.loggingInPleaseWait));

    getLoginObservable(editTextUsername.getText().toString(), password)
            .map(response -> {
                if (response.result) {
                    getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
                }
                return response;
            })
            .flatMap(response -> {
                return getFetchDataObservable();
            })
            .subscribe(res -> {
                dismissProgress();
                if (res.result) {
                    saveInformation(password, res);
                } else {
                    ConstantsMethods.showOkButtonDialog(getContext(), res.message, null);
                }
            }, e -> {
                dismissProgress();
                if (e instanceof NoInternetConnectionException) {
                    ConstantsMethods.showOkButtonDialog(getContext(), getString(R.string.noInternetConnection), null);
                }
                Log.e(LoginFragment.class.getSimpleName(), e.getMessage());
            });
}



    private Observable<WsResponse<SecurityQuestion>> getLoginObservable(String userName, String password) {
        return Observable.<WsResponse<SecurityQuestion>>create(subscriber -> {
            getPresenter().doLogin(getActivity(), userName, password, appType,
                    new Callback<Void, WsResponse<SecurityQuestion>>() {
                        @Override
                        public Void callback(final WsResponse<SecurityQuestion> param) {
                            subscriber.onNext(param);
                            return null;
                        }
                    });
        });
    }

    private Observable<WsResponse<PatientDataProfile>> getFetchDataObservable() {
        return Observable.create(subscriber -> {
            new AfPatientsPresenter().fetchPatientData(getContext(), emailAddress, "", new Callback<Void, WsResponse<PatientDataProfile>>() {
                @Override
                public Void callback(WsResponse<PatientDataProfile> param1) {
                    subscriber.onNext(param1);
                    subscriber.onComplete();
                    return null;
                }
            });
        });
    }

As much i know RxJava, I can figure out that getLoginObservable(editTextUsername.getText().toString(), password) observable send response to map (map(response -> { ... }) and this map return response to flatmap (flatMap(response -> { ... }) and its response is sent to subscriber. Here i am just lost that how can i skip (second network call)flatmap flatMap(response -> { ... } to send response directly to subscriber in case of login failure.

Upvotes: 1

Views: 1811

Answers (1)

LordRaydenMK
LordRaydenMK

Reputation: 13321

instead of:

.map(response -> {
                if (response.result) {
                    getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
                }
                return response;
            })

you can use:

flatMap(response-> {
    if (response.result) {
        getPresenter().saveUserDetails(getContext(), emailAddress, true, response.dataObject.questionId, response.dataObject.question);
        return Observable.just(response);
    } else {
        return Observable.error(new Exception("Login failed")); // or maybe some LoginFailedException() you can reuse
    }
})

Upvotes: 4

Related Questions