Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JohnSundell/85fd3b0f11e20cf65b442cdffd13926b to your computer and use it in GitHub Desktop.
Save JohnSundell/85fd3b0f11e20cf65b442cdffd13926b to your computer and use it in GitHub Desktop.
class DataLoader {
enum Result {
case data(Data)
case error(Error)
}
func load(from url: URL, completionHandler: @escaping (Result) -> Void) {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
return completionHandler(.error(error))
}
completionHandler(.data(data ?? Data()))
}
task.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment