August 23, 2021
Swift Concurrency
The star of the show at WWDC 2021 for developers was news of built in concurrency to the Swift language. This has been a long awaited set of features and proves to aid developers in writing efficient, correct, and easier to reason about concurrent code.
Async/Await
It all starts with the introduction of async
and await
. These keywords are used to express concurrent intent. Functions marked with async
can suspend execution while waiting for resources to be available. When calling an async
function you must await
the result and acknowledge that calling such a function may suspend the current call stack. This closely resembes, and is even composable with, throwing functions in the way that we must ackowledge the possibility of errors by using try
.
Structured Concurrency
Have you been utilizing any GOTO
statements in your Swift code? Assuredly not because Swift, like most other modern languages, follow a paradigm called Structured Programming. This includes for loops, if statements, functions… basically everything we use to give our program clarity and modularity. New to Swift is a paradigm called Structured Concurrency!
Imagine a utopia where you kick off network fetches, wait on long running data transforms, and update the UI all with various conditionals, loops,… all at the same time and all while in the same block of code. IT. IS. INCREDIBLE.
Actors
Data races are a common issue when building an application utilizing asynchrony at any scale. Just the thought of finding and debugging a data race might have sent chills down your back. Swift 5.5 brings along a new entity, actors, which protect against data races and make reasoning about accessing shared state very easy.
❖ ❖ ❖