RxJava2问题:Zip并行处理多个服务时如何处理错误? [英] RxJava2 question: How Zip handles error when calling multiple services in parallel?

查看:1937
本文介绍了RxJava2问题:Zip并行处理多个服务时如何处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有3个服务,employeeTitle,employeeName,
employeeLocation。如果所有服务三个服务抛出错误
employeeTitle,employeeName和employeeLocation只服务
抛出1个错误?根据文档,这应该是
行为这是正确的吗?如果只有employeeTitle抛出
错误而其他服务成功,会发生什么?根据我的理解
如果employeeTitle抛出异常,那么zip仍然只会抛出1
异常,而这个异常将是employeeTitle的服务
错误。我是否在正确的轨道上?

Suppose we have 3 services, employeeTitle, employeeName, employeeLocation. If all services three services throw error employeeTitle, employeeName and employeeLocation will service only throw 1 error? According to the documentation this should be the behavior is this correct? What happens if only employeeTitle throws error and the other services are successful? As per my understanding if employeeTitle throws an exception then zip will still throw 1 exception only and this exception will be employeeTitle's service error. Am I in the right track?

- 在我的下面的代码中,我期待zip错误出现在getEmployeeInfo()

-In my code below, I am expecting the zip error to come in the first onErrorResumeNext inside getEmployeeInfo()

Single<Model> getCompositeEmployeeInfo(){ 
  return Single.zip(
     api.employeeTitle()
       .subscribeOn(Schedulers.newThread()),
     api.employeeName()
       .subscribeOn(Schedulers.newThread()),
     api.employeeLocation()
       .subscribeOn(Schedulers.newThread()),
     new Function3<EmployeeTitle, EmployeeName, EmployeeLocation>() {

     @public Model apply(EmployeeTitle empTitle, EmployeeName empName,
                         EmployeeLocation empLocation){
       //some logic
     }

Single<Model2> getEmployeeInfo(){  
return getCompositeEmployeeInfo()
        .observeOn(AndroidSchedulers.mainThread())
        .onErrorResumeNext(throwable-> { 
           return Single.error(throwable}//I am expecting only 1 error(if 
            //more than one error is thrown) from 
            //zip, either employeeTitle, employeeName or employeeLocation.
         )
         .flatMap(model -> {
                 //some logic
           return getEmployeePreference();
                    .observeOn(AndroidSchedulers.mainThread())
                    .onErrorResumeNext(throwable -> {
                       return Single.error(throwable);
                    })
                   .flatMap(employeePreference -> {
                      //some logic
                      return Single.just(result);
           });  } }


推荐答案

被动合同声明可以只有一个终止条件, onError onComplete 。当您将多个观察者链与 zip()组合时,来自三个观察者链的终止条件合并为一个。

The "reactive contract" states that there can only be one terminating condition, either onError or onComplete. When you combine multiple observer chains with zip(), termination conditions from the three observer chains are combined into one.

如果第一个观察链发出错误,则仍有可能在其他两个观察者链中引发错误。但是, zip()运算符仅发出第一个错误。另外,由于观察者链在不同的线程上运行,因此会有竞争来查看报告的错误;这是非确定性的。

If an error is raised by the first observer chain, there is still a possibility that errors can be raised in the other two. However, only the first error is emitted by the zip() operator. In addition, since the observer chains run on separate threads, there will be a race to see which error gets reported; it is non-deterministic.

观察链链终止后或者不再存在时出现的错误处理下游错误,传递给全局处理程序。

Errors that are raised in an observer chain after the chain has terminated, or when there is no longer a way to handle the error down-stream, are passed to a global handler.

这篇关于RxJava2问题:Zip并行处理多个服务时如何处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆