Hacker News new | ask | show | jobs
by robsws 1444 days ago
I don't really know Swift, but does the following line from that site make any sense?

"And with Swift concurrency, it’s easy to request weather data with just a few lines of code."

4 comments

Without Swift Concurrency, you'd write something like this (pseudocode):

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()

They're saying that since it supports async/await the developer doesn't need to deal with callbacks/whatever. That line is accurate ("just a few lines of code") but also marketing.
I think it just says it‘s easy to request weather data concurrently and to integrate that into other concurrency stuff, thanks to Swift.

But I‘ve never used Swift so I don‘t know.

Basically just saying you can use async/await to fetch the data, I think