Created
July 12, 2021 17:04
-
-
Save atulkhatri/bb8c841328f2d7fa8812967c9dba08f6 to your computer and use it in GitHub Desktop.
tvOS Bootcamp Network Manager 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func executeRequest<T: Codable>(request: URLRequest, completion: ((T?, Error?) -> Void)?) { | |
let session = URLSession(configuration: .default) | |
let dataTask = session.dataTask(with: request) { (data, response, error) in | |
guard let data = data else { | |
completion?(nil, error) | |
return | |
} | |
if let decodedResponse = try? JSONDecoder().decode(T.self, from: data) { | |
DispatchQueue.main.async { | |
completion?(decodedResponse, nil) | |
} | |
} else { | |
completion?(nil, NetworkError.invalidData) | |
} | |
} | |
dataTask.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment