1
votes

I have below scenario, where, once I get a service response from zip(getHardwareInfo) I need to make another service call (getSoftwareInfo()). So basically getSoftwareInfo() gets called only if getHardwareInfo() is successful.

-Now I want to add a condition where getSoftwareInfo() gets called not only after getHardwareInfo is successful but also if a condition is met, i.e. isChristmas(). I have changed my code from version 1 to version 2 and it works. I have debugged it and seems to work fine. Not sure if I am doing it correctly though since I am new to rxjava. Any help will be appreciated.

Version 1:

public Single<ComputerData> getComputerData(){
 return getHardwareInfo().observeOn(AndroidSchedulers.mainThread())
           .onErrorREsumeNext(throwable->{ 
            return Single.error() 
        }).flatMap(hardwareInfo->{
           //do something with hardwareInfo
           return getSoftwareInfo();
        .observeOn(AndroidSchedulers.mainThread())
        .onErrorResumeNext(throwable->{
         return Single.error());
         })
         .flatMap(softwareInfo->{
           //do something with softwareInfo
         });

version 2:

public Single<ComputerData> getComputerData(){
 return getHardwareInfo().observeOn(AndroidSchedulers.mainThread())
           .onErrorREsumeNext(throwable->{ 
            return Single.error() 
        }).flatMap(hardwareInfo->{
           //do something with hardwareInfo
          if(isChristmas()){
           return getSoftwareInfo()
                 .observeOn(AndroidSchedulers.mainThread());
                 .onErrorResumeNext(throwable->{
                  return Single.error());
                  })
                  .flatMap(sofwareInfo->{
                  //do something with softwareInfo
                  });
          } else{
                //do something else.
           }
         });
1

1 Answers

0
votes

The onErrorResumeNext is not needed if you just want to return the error.

This is it:

getHardwareInfo()
    .flatMap(hi -> getSoftwareInfo())
    .filter(si -> isChristmas());

You just need to add the proper schedulers