I am making a network call using rxJava2, and based on the response (either success or error), I have to move my work forward on UI thread.
I have written the code below. It seems working fine.
WApi wApi = ServiceGenerator.createService(WApi.class, sURL);
dataManager = InventoryModule.getDataManager();
rx.Observable<GetFeature> getFeatureObservable =
dataManager.executeGetFeature(caseId, wrapperApi);
if (getCV2FeatureObservable != null) {
try {
getFeatureObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> {
Log.e(TAG, "Err::" + throwable.getMessage());
// init default values because of error response
initDefaultValues();
// No data to use from WS call. Move forward with
//cookie from SSO auth
cookieReceived(userID, cookieData, patchNo);
})
.onErrorResumeNext(rx.Observable.empty())
.subscribe(getFeature -> {
// use the data from WS response
processAndUpdateFeature(getFeature);
// move forward with cookie from SSO auth
cookieReceived(userID, cookieData, patchNo);
});
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
}
Still I need opinions, Am I doing it right? Am I missing something? or can I use other operators and make it better? the way I am placing my UI work into corresponding operators, will it work properly in both error or success response?