gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:34:55 29 4
gpt4 key购买 nike

我正在使用 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 时,如何重试我的医院请求?

最佳答案

您应该使用 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)
}
}

关于swift - Rxswift + Moya + Moya-ObjectMapper 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327800/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com