Skip to content

Instantly share code, notes, and snippets.

@atulkhatri
Created July 12, 2021 17:04
Show Gist options
  • Save atulkhatri/bb8c841328f2d7fa8812967c9dba08f6 to your computer and use it in GitHub Desktop.
Save atulkhatri/bb8c841328f2d7fa8812967c9dba08f6 to your computer and use it in GitHub Desktop.
tvOS Bootcamp Network Manager 3
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