Skip to content

Instantly share code, notes, and snippets.

@candostdagdeviren
Last active November 29, 2018 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save candostdagdeviren/96012003a574ce01abe1bda0253d2bfb to your computer and use it in GitHub Desktop.
Save candostdagdeviren/96012003a574ce01abe1bda0253d2bfb to your computer and use it in GitHub Desktop.
Swift Post Concurrency Blog Post Code Example
/* 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