首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rxswift + Moya + Moya-ObjectMapper错误处理

Rxswift + Moya + Moya-ObjectMapper错误处理
EN

Stack Overflow用户
提问于 2016-09-05 17:41:20
回答 1查看 1.3K关注 0票数 1

我正在使用带有Rxswift的MoyaMoya-ObjectMapper发出网络请求。

我的网络请求如下。

代码语言:javascript
复制
let provider = RxMoyaProvider<APIClient>()

requestHospitalButton.rx_tap
    .withLatestFrom(hospitalCode)
    .flatMapLatest { [unowned self] code in
        self.provider.request(.Hospital(code: code))
     }
     .mapObject(Hospital)
     .subscribe { [unowned self] event in
         switch event {
         case .Next(let hospital):
             // success
         case .Error(let error):
             // error
         default: break
         }
     }
     .addDisposableTo(rx_disposeBag)

如果出现此错误,我的医院请求Observable将终止,并且我再也不能发出我的医院请求。

requestHospitalButton被点击时,如何重试我的医院请求?

EN

回答 1

Stack Overflow用户

发布于 2016-09-05 17:57:32

您应该使用文档记录的here中的retryWhen

代码语言:javascript
复制
extension ObservableType {

    /**
    Repeats the source observable sequence on error when the notifier emits a next value.
    If the source observable errors and the notifier completes, it will complete the source sequence.
    - seealso: [retry operator on reactivex.io](http://reactivex.io/documentation/operators/retry.html)

    - parameter notificationHandler: A handler that is passed an observable sequence of errors raised by the source observable and returns and observable that either continues, completes or errors. This behavior is then applied to the source observable.
    - returns: An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully or is notified to error or complete.
    */
    public func retryWhen<TriggerObservable: ObservableType>(_ notificationHandler: @escaping (Observable<Swift.Error>) -> TriggerObservable)
        -> Observable<E> {
            return RetryWhenSequence(sources: InfiniteSequence(repeatedValue: self.asObservable()), notificationHandler: notificationHandler)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39327800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档