Skip to content

Instantly share code, notes, and snippets.

@J7mbo
Created August 8, 2018 14:38
Show Gist options
  • Save J7mbo/e9ff23be75e91e4060960ce9ab7c03f8 to your computer and use it in GitHub Desktop.
Save J7mbo/e9ff23be75e91e4060960ce9ab7c03f8 to your computer and use it in GitHub Desktop.
struct JsonDownloader
{
public func downloadContentsOfUrl(
url: URL,
onUrlRetrieved: @escaping (_ url: URL, (UIImage) -> Void) -> Void,
onImageDownloaded: @escaping (_ image: UIImage) -> Void
) -> Void {
// This async call will be thrown onto a thread and then this function will return immediately
DispatchQueue.global(qos: .background).async {
let json = try! Data(contentsOf: url)
let jsonData = try! JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
// Calling our first callback, and passing in the second, violating LoD
onUrlRetrieved(URL(string: jsonData["image_url"] as! String)!, onImageDownloaded)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment