Created
August 8, 2018 14:38
-
-
Save J7mbo/e9ff23be75e91e4060960ce9ab7c03f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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