Search code examples
androidrx-java2rx-android

What is the best way to make a rxJava netwrok call and move work forward on UI thread based on response


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?


Solution

  • The only questionable choice is all the complications you're doing on errors.

    instead of using doOnError + onErrorResumeNext I suggest you move your logic to the Subscriber:

    getFeatureObservable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(getFeature -> {
                // use the data from WS response
                processAndUpdateFeature(getFeature);
                // move forward with cookie from SSO auth
                cookieReceived(userID, cookieData, patchNo);
            }, { 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);
            });
    
    

    Your thread switching (subscribeOn and observeOn) is fine.


    EDIT: One more thing: Unless the processAndUpdateFeature or initDefaultValues or cookieReceived can throw an error, the try-catch block seems unnecessary.