"And with Swift concurrency, it’s easy to request weather data with just a few lines of code."
"And with Swift concurrency, it’s easy to request weather data with just a few lines of code."
GetWeather() { results in
// Do something with the results
UpdateUI(results)
}The part in the curly braces is a closure/completion handler. The GetWeather call is not blocking, so it runs on another thread and then it calls your completion code when it's done.
This looks fine in a small example, but it quickly gets gnarly when you need to pipe those results to something else, or you can also end up deep in many completion handlers.
With the new concurrency model it's just this
let results = await GetWeather ()
UpdateUI(results)
Now let's say your UpdateUI call was also non blocking:
GetWeather() { results in
UpdateUI(results) {
DoSomethingElse ()
}
}Which just becomes this
let results = await GetWeather ()
await UpdateUI(results)
DoSomethingElse()
But I‘ve never used Swift so I don‘t know.