QUESTION OF THE WEEK
“
Which of these points will run on the main thread?
This week, I decided to follow up on one of my social media posts on threading in Swift Concurrency. I've had quite a few replies and discussions on the correct answer, but most of all, I realized many of us still have a hard time understanding how async functions get dispatched to threads.
It resulted in a new thread on the Swift Forums to clarify things, but also a mention of SE-461 Run nonisolated async functions on the caller's actor by default. This proposal is one I want to highlight in this week's newsletter.
The Swift core team recognizes the issues we're running into and SE-461 is one of the improvements they're making. To quote the proposal:
Swift's general philosophy is to prioritize safety and ease-of-use over performance, while still providing tools to write more efficient code. The current behavior of nonisolated async functions prioritizes main actor responsiveness at the expense of usability.
This proposal changes the behavior of nonisolated async functions to run on the caller's actor by default, and introduces an explicit way to state that an async function always switches off of an actor to run.
It's important to realize that we're still in the early days of Swift Concurrency. Last week's release of Swift 6.1 introduced several improvements, and there are more to come. This is simply a result of real-world usage and collected feedback—we're all working together to create a more solid concurrency framework.
My contribution can be found in my free articles on Swift Concurrency, but soon, I'll have an even bigger role in helping you become an expert on anything related to concurrency. Hundreds of developers are already on the waitlist, and I'd love for you to join the concurrency course waitlist, too.