Rxjava Error Handling

在Rxjava的使用过程中我们有时候会遇到Observable层的error or exception。当然了,对于这些错误,Rxjava给我们提供了一些方法来处理这些事情,来避免我们的业务逻辑发生混乱。所以这篇博文就给大家带来这些错误处理的机制是啥样的,其实就是几个方法的使用。

onExceptionResumeNext

Instructs an ObservableSource to pass control to another ObservableSource rather than invoking
* {@link Observer#onError onError} if it encounters an {@link java.lang.Exception}.
* <p>
* This differs from {@link #onErrorResumeNext} in that this one does not handle {@link java.lang.Throwable}
* or {@link java.lang.Error} but lets those continue through.

这个方法注释的大概意思就是在遇到exception的时候不会去invoke observer的onError,而是会将当前ObservableSource的控制权交给另外一个ObservableSource。其实就是会重新创建一个Observable. 还有就是与onErrorResumeNext不同的是onExceptionResumeNext 处理的是Exception,onErrorResumeNext处理的是Error

结合这个图应该是比较好理解这个方法的含义了。下面在通过例子加深下印象。

这下应该可以很清楚的知道这个方法的大概作用了。

onErrorResumeNext

这个就不用多说了,前面已经讲过了

doOnError

Modifies the source ObservableSource so that it invokes an action if it calls onError.

这个注释稍微有点抽象,如果没有去看过源码的话,但从这个注释来说,没搞懂它是干嘛的。这里大概说下,就是有点类似doOnNext,就是在consumer的error被调用的时候,会触发doOnError。

再结合这张图应该比较好理解的。

onErrorReturnItem

Instructs an ObservableSource to emit an item (returned by a specified function) rather than invoking
* {@link Observer#onError onError} if it encounters an error.

大概就是在遇到error的时候,会 emit an item 而不会触发 onError.

就像你看到的。

onErrorReturn

Instructs an ObservableSource to emit an item (returned by a specified function) rather than invoking
* {@link Observer#onError onError} if it encounters an error.

我这里没有贴错,看着跟onErrorReturnItem是一样的。其实onErrorReturnItem内部调用了onErrorReturn。这里你可以提供一个function来返回你想要的数据。

其实onErrorReturnItem内部已经提供了一个function。这里不过多叙述,这里只描述它是干嘛的,怎么用的。不会涉及到源码。

可以看到可以自己处理function.

retry

Returns an Observable that mirrors the source ObservableSource, resubscribing to it if it calls {@code onError}
* (infinite retry count).

这个方法会一直尝试重新subscribe,无限循环。所以建议使用带有超时时间跟重试次数的那个方法。

这里就不贴代码了,结果就是一直在重试。

retryWhen

Returns an Observable that emits the same values as the source ObservableSource with the exception of an
* {@code onError}. An {@code onError} notification from the source will result in the emission of a
* {@link Throwable} item to the ObservableSource provided as an argument to the {@code notificationHandler}
* function. If that ObservableSource calls {@code onComplete} or {@code onError} then {@code retry} will call
* {@code onComplete} or {@code onError} on the child subscription. Otherwise, this ObservableSource will
* resubscribe to the source ObservableSource.

这里的大概意思就是retryWhen会根据notificationHandler的处理来决定是否要重新订阅原来的Observable。如果这个Observable发射了一个数据,就重新订阅,如果发射的是onError通知,它就会将这个通知传递给观察者然后终止。

retryWhen

示例代码

 

Observable.create((Subscriber<? super String> s) -> {
      System.out.println("subscribing");
      s.onError(new RuntimeException("always fails"));
  }).retryWhen(attempts -> {
      return attempts.zipWith(Observable.range(1, 3), (n, i) -> i).flatMap(i -> {
          System.out.println("delay retry by " + i + " second(s)");
          return Observable.timer(i, TimeUnit.SECONDS);
      });
  }).toBlocking().forEach(System.out::println);

output

subscribing
delay retry by 1 second(s)
subscribing
delay retry by 2 second(s)
subscribing
delay retry by 3 second(s)
subscribing

 

经过这几个方法的讲解,相信大家应该对Rxjava的错误处理能够认识的多一点。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值