2016-09-05 4 views
1

네트워크 요청을하기 위해 Rxswift에서 MoyaMoya-ObjectMapper을 사용 중입니다.Rxswift + Moya + Moya-ObjectMapper 오류 처리

내 네트워크 요청은 다음과 같습니다.

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을 두드렸 으면 병원 요청을 어떻게 다시 시도 할 수 있습니까?

답변

1

당신은 here 설명되어 retryWhen 사용해야합니다 :

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