/// 网络请求&重用机制
/// - Parameters:
/// - target: target description
/// - model: model description
/// - delay: delay description
/// - max: max description
/// - callbackQueue: callbackQueue description
/// - progress: progress description
/// - completion: completion description
static func cz_request<T: BaseMappable>(target: TargetType,
model: T.Type,
delay: Double = 3,
max: Int = 3,
callbackQueue: DispatchQueue? = .none,
progress: ProgressBlock? = .none,
completion: @escaping (_ result: Result<T, CZError>) -> Void) {
cz_provider.request(MultiTarget(target), callbackQueue: callbackQueue, progress: progress) { (result) in
switch result {
case let .success(response):
do {
let _ = try response.filterSuccessfulStatusCodes()
} catch {
completion(.failure(CZError("数据获取失败")))
}
do {
let jsonDict = try response.mapJSON()
if let model = Mapper<T>(context: nil).map(JSONObject: jsonDict) {
completion(.success(model))
} else {
completion(.failure(CZError("数据解析失败")))
}
} catch {
completion(.failure(CZError("json格式有误")))
}
case .failure(_):
if max == 1 {
completion(.failure(CZError("似乎已断开与互联网的连接")))
} else { // 重试
DispatchQueue(label: "com.request.queue", attributes: .concurrent).asyncAfter(deadline: DispatchTime.now() + delay) {
cz_request(target: target, model: model, delay: delay, max: max - 1, completion: completion)
}
}
}
}
}
/// 网络请求&重用机制(RxSwift)
/// - Parameters:
/// - target: target description
/// - model: model description
/// - delay: delay description
/// - max: max description
/// - callbackQueue: callbackQueue description
/// - progress: progress description
static func cz_request<T: BaseMappable>(target: TargetType,
model: T.Type,
delay: Double = 3,
max: Int = 3,
callbackQueue: DispatchQueue? = .none,
progress: ProgressBlock? = .none) -> Single<T> {
return Single.create { single in
let cancellableToken = cz_provider.request(MultiTarget(target), callbackQueue: callbackQueue, progress: progress) { result in
switch result {
case let .success(response):
do {
let _ = try response.filterSuccessfulStatusCodes()
} catch {
single(.error(CZError("数据获取失败")))
}
do {
let jsonDict = try response.mapJSON()
if let model = Mapper<T>(context: nil).map(JSONObject: jsonDict) {
single(.success(model))
} else {
single(.error(CZError("数据解析失败")))
}
} catch {
single(.error(CZError("json格式有误")))
}
case .failure(_):
single(.error(CZError("似乎已断开与互联网的连接")))
}
}
return Disposables.create {
cancellableToken.cancel()
}
}.retryWhen(retryHandler(delay: Int(delay),max: max))
}
/// 重试
/// - Parameter delay: 每几秒重试
/// - Parameter max: 最大重试次数
public func retryHandler(delay: Int = 5, max: Int = 5) -> ((Observable<Error>) -> Observable<Int>) {
return { error in
return error.enumerated().flatMap { (index, error) -> Observable<Int> in
guard index < max else {
return Observable.error(error)
}
return Observable<Int>.timer(.seconds(delay), scheduler: MainScheduler.instance)
}
}
}
iOS Moya网络重试机制
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...