如何重试失败的网络请求
How to retry network request that has failed
我对 Swift/iOS 有点陌生,一直在使用 URLSession 对 REST API 进行网络调用。这是我用来发出网络请求的(稍微清理过的)代码:
enum ApiRequestError: Error {
case statusCodeOtherThan200(statusCode: Int)
// other error cases will be added here
}
enum ApiResponse {
case success(Data)
case failure(Error)
}
typealias myCompletionHandler = (ApiResponse) -> ()
func sampleRestCall(token: String, completionHandler: @escaping myCompletionHandler) {
let url = URL(string: "https://www.server.com")
// removed headers, parameters, etc. for brevity
let request = URLRequest(url: url!)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
myCompletionHandler(.failure(error))
return
} else if let data = data, let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
myCompletionHandler(.success(data))
} else {
print("Server returned unexpected response: \(response.statusCode) - (\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode)))")
completionHandler(.failure(ApiRequestError.statusCodeOtherThan200(statusCode: response.statusCode)))
}
}
}.resume()
}
func testSampleRestCall() {
sampleRestCall(token: "12345678") { apiResponse in
switch apiResponse {
case .success(let data):
print("JSON response:")
if let jsonResponse = String(data: data, encoding: String.Encoding.utf8) {
print("\(jsonResponse)")
}
case .failure(let error):
print("Failure! \(error)")
}
}
}
代码运行良好,但我想知道如果失败如何重试网络请求。我知道一个请求可能会因为很多不同的原因而失败,但是为了这个问题,我想关注一个失败只是暂时的并且可以通过几秒钟后重试请求来解决的场景。理想情况下,我想要某种 "retry" 包装器,它允许我指定在放弃并通知用户之前应重试调用 X 次。作为一项附加功能,我可能会在每次重试之间插入一小段等待时间。我发现有几个图书馆可以做这样的事情,但我想了解如何自己做,而不是仅仅依赖图书馆。如果有人对实现这种功能有建议,我将不胜感激!
试试这个
case .failure(let error):
print("Failure! \(error)")
if(self.counter<3)
{
self.testSampleRestCall()
counter = counter+1
}
else
{
// notify user check if this a thread other than main to wrap code in main queue
}
}
我对 Swift/iOS 有点陌生,一直在使用 URLSession 对 REST API 进行网络调用。这是我用来发出网络请求的(稍微清理过的)代码:
enum ApiRequestError: Error {
case statusCodeOtherThan200(statusCode: Int)
// other error cases will be added here
}
enum ApiResponse {
case success(Data)
case failure(Error)
}
typealias myCompletionHandler = (ApiResponse) -> ()
func sampleRestCall(token: String, completionHandler: @escaping myCompletionHandler) {
let url = URL(string: "https://www.server.com")
// removed headers, parameters, etc. for brevity
let request = URLRequest(url: url!)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
myCompletionHandler(.failure(error))
return
} else if let data = data, let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
myCompletionHandler(.success(data))
} else {
print("Server returned unexpected response: \(response.statusCode) - (\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode)))")
completionHandler(.failure(ApiRequestError.statusCodeOtherThan200(statusCode: response.statusCode)))
}
}
}.resume()
}
func testSampleRestCall() {
sampleRestCall(token: "12345678") { apiResponse in
switch apiResponse {
case .success(let data):
print("JSON response:")
if let jsonResponse = String(data: data, encoding: String.Encoding.utf8) {
print("\(jsonResponse)")
}
case .failure(let error):
print("Failure! \(error)")
}
}
}
代码运行良好,但我想知道如果失败如何重试网络请求。我知道一个请求可能会因为很多不同的原因而失败,但是为了这个问题,我想关注一个失败只是暂时的并且可以通过几秒钟后重试请求来解决的场景。理想情况下,我想要某种 "retry" 包装器,它允许我指定在放弃并通知用户之前应重试调用 X 次。作为一项附加功能,我可能会在每次重试之间插入一小段等待时间。我发现有几个图书馆可以做这样的事情,但我想了解如何自己做,而不是仅仅依赖图书馆。如果有人对实现这种功能有建议,我将不胜感激!
试试这个
case .failure(let error):
print("Failure! \(error)")
if(self.counter<3)
{
self.testSampleRestCall()
counter = counter+1
}
else
{
// notify user check if this a thread other than main to wrap code in main queue
}
}