Last active
November 29, 2018 14:15
-
-
Save candostdagdeviren/96012003a574ce01abe1bda0253d2bfb to your computer and use it in GitHub Desktop.
Swift Post Concurrency Blog Post Code Example
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
/* I put the first DispatchQueue method intentionally to indicate that we're on | |
the background queue. Just to support the explanation for beginner developers | |
This is not necessary in real-life, URLSession is thread-safe. Whenever you call URLSession | |
it'll start executing on background. */ | |
DispatchQueue.global().async { | |
let url = URL(string: "https://theswiftpost.co/") | |
if let url = url { | |
DispatchQueue.main.sync { | |
showLoadingIndicator() // UI Operation; has to run on the main queue | |
} | |
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if error != nil { | |
DispatchQueue.main.sync { | |
showErrorAlert(error) // UI operation; has to run on the main queue!! | |
} | |
} else if let data = data { | |
debugPrint(usableData) //JSONSerialization | |
} | |
debugPrint(“Completed”) | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment