Home > Software engineering >  Swift Async Networking. How do I RETRY on error?
Swift Async Networking. How do I RETRY on error?

Time:01-12

I have created a generic networking function using async which is working fine, but I would like to be able to retry the request if it throws an error, if the Token is expired for example. How would I do this without having to change all my DO Catch?

func request<T: Codable>(_ endpoint: Endpoint) async throws -> T {
    
    guard let url = endpoint.url else {
        throw NetworkError.badURL
    }
    
    guard let token = idToken else {
        throw NetworkError.invalidToken
    }
    
    let request = buildRequest(from: url, methodType: endpoint.methodType, idToken: token)
    
    let (data, response) = try await URLSession.shared.data(for: request)
    guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
        
        //RETRY X AMOUNT OF TIMES <<<
        
        throw NetworkError.invalidResponse
    }
    
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    guard let result = try? decoder.decode(T.self, from: data) else {
        throw NetworkError.decodingError
    }
    
    print("Result: \(result)")
    
    return result
}

CodePudding user response:

If you want to do this, I would suggest writing a rendition of data(for:delegate:) that wraps the retry logic, e.g.:

extension URLSession {
    func data(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil, maxRetries: Int) async throws -> (Data, URLResponse) {
        for _ in 0 ..< maxRetries {
            let (data, response) = try await data(for: request, delegate: delegate)
            guard let response = response as? HTTPURLResponse else {
                throw URLError(.badServerResponse)
            }
            if 200..<300 ~= response.statusCode {     // as an aside, note, any 2xx response is success
                return (data, response)
            }
        }

        throw NetworkError.invalidResponse
    }
}

Then, you can replace:

let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
    
    //RETRY X AMOUNT OF TIMES <<<
    
    throw NetworkError.invalidResponse
}

With:

let (data, response) = try await URLSession.shared.data(for: request, maxRetries: 3)
  • Related