【问题标题】:Rxswift + Moya + Moya-ObjectMapper Error handlingRxswift + Moya + Moya-ObjectMapper 错误处理
【发布时间】:2017-01-12 15:49:48
【问题描述】:

我正在使用 MoyaMoya-ObjectMapper 和 Rxswift 来发出网络请求。

我的网络请求如下。

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 被点击时,如何重试我的医院请求?

【问题讨论】:

    标签: swift rx-swift reactivex moya


    【解决方案1】:

    您应该使用retryWhen,记录在here

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2016-03-28
      • 2019-03-17
      • 2017-04-27
      • 2023-03-12
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多