所以我正在尝试为我的网络调用(第一层)构建一个重试功能。这是功能:
func retry<T>(_ attempts: Int, task: @escaping (_ success: @escaping (T) -> Void, _ failure: @escaping (String) -> Void) -> Void, success: @escaping (T) -> Void, failure: @escaping (String) -> Void) {
task({ (obj) in
success(obj)
}) { (error) in
print("Error retry left \(attempts)")
if attempts > 1 {
self.retry(attempts - 1, task: task, success: success, failure: failure)
} else {
failure(error)
}
}
}
实现是这样的:
func refreshSession(success: @escaping () -> Void, failure: @escaping (String) -> Void) {
cameraProtocols?.refreshSession( success: {
print("calling serverping")
self.timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { (timer) in
self.keepAlive(session: CameraManager.session, success: {
print("serverping succsess")
success()
}, failure: { (error) in
print(error)
failure(error)
})
})
}, failure: { (error) in
print(error)
failure(error)
})
}
func keepAlive(session: String, success: @escaping () -> Void, failure: @escaping (String) -> Void) {
cameraProtocols?.keepAlive(session: CameraManager.session, success: {
print("server ping done!")
NotificationCenter.default.post(name: Notification.Name(rawValue: "pingSuccess"), object: nil)
success()
}, failure: { (error) in
print(error)
self.retry(3, task: { (success, failure) in
self.refreshSession(success: success, failure: failure)
}, success: { (success) in
print("refresh succsed from retry")
success
}, failure: { (e) in
print("refresh failed from retry: \(e)")
//TODO - handle error logic when to procced with the failure closure
failure(error)
})
NotificationCenter.default.post(name: Notification.Name(rawValue: "faildPing"), object: nil)
})
}
当我故意使keepalive调用失败时,重试不会按预期工作,并且永远不会停止。
关于新功能或如何修复的任何建议?