The latest Swift concurrency features added to Swift version 5.5 was one of the hottest topics at Apple’s Worldwide Developers Conference (WWDC) 2021. If you’re unfamiliar with Swift, it is Apple’s preferred programming language for developing native iOS mobile apps.
While Swift is an open-source programming language, it is regularly updated and maintained by the development team at Apple. New features in Swift have a big impact on the future capabilities of iOS apps, so when new releases are announced, there is generally a lot of excitement.
This year, iOS developers and enthusiasts were treated to improved concurrency support features like async/await and actors. Let’s explore the exciting new Swift concurrency features in more detail to give you a broad overview of what you can expect from the latest version of Swift.
What is Concurrency?
Concurrency is a development term used to refer to the combination of asynchronous and parallel code. Asynchronous code allows for tasks to be suspended and resumed at a later point. This capability creates a more fluid User Experience because it allows apps to complete short-term tasks like updating User Interface elements while working on long-term tasks like retrieving data.
Parallel code refers to multiple pieces of code running simultaneously. This gives users the ability to execute multiple tasks at the same time. The combination of asynchronous and parallel code, or concurrency, is essential to the function of modern mobile apps.
Swift concurrency gives iOS developers the ability to create apps that can perform multiple tasks at a time and suspend operations that need to wait for an external system. Swift’s concurrency features also make writing asynchronous, parallel code more memory-safe.
Improvements to Swift Concurrency
Despite the powerful advantages Swift concurrency features give iOS developers, in previous release versions of Swift, problems could arise when too many asynchronous tasks were used, error handling was required, or the control flow between asynchronous calls got too complicated.
In order to provide a more efficient, convenient, and safer method of concurrency programming, Apple has introduced two new concurrency features to Swift 5.5:
- Async/await
- Actors
We’ll simplify some of the complexities associated with these two new Swift concurrency features to provide you with a general overview and understanding of how these additions improve the capabilities of the programming language.
Async/await
One of the major issues associated with asynchronous code is that it lacks the easy, concrete structure that is associated with synchronous code. Asynchronous code is already complicated, but when you add multiple asynchronous tasks to a program, managing control flow can get very difficult.
The new async/await concurrency feature added to Swift 5.5 allows developers to write and run complex asynchronous code as if it were synchronous. Here is a simple explanation of how this complicated operation functions. Asynchronous tasks and functions are labeled with the new syntax keyword async, and they are called by using the syntax keyword await.
This model of structured concurrency is very similar to the way other programming languages like JavaScript and C# handle asynchronous tasks. While individual operations using async/await are asynchronous, they can still be executed sequentially. This ensures that functions do not block threads while they are waiting to be completed.
Here are a few of the early reasons iOS app developers are excited about the new async/await concurrency features of Swift:
- Easier to debug and explore the complexities of the code
- Better performance for asynchronous code
- Provides a solid foundation for future concurrency features to be added
Asynchronous programming is an important part of modern mobile app development. The introduction of the async/await syntax in Swift gives developers the ability to create complex logic with asynchronous tasks while using traditional control flow mechanisms.
Actors
Swift, like most other programming languages, uses classes as general-purpose building blocks for iOS apps. Classes are deemed “thread-safe” if they won’t cause your app to crash. Asynchronous code can cause issues when two or more pieces of code that are running concurrently try to read, write, or update the same class at the same time.
Developers refer to two or more pieces of code trying to update the same class as a data race. Swift has a built-in feature called thread sanitizer that helps iOS developers find and eliminate data races in their code. However, this feature is not foolproof, and crashes can still happen at app runtime. Plus, you never know when new data race issues will be introduced in your code during the course of development.
Swift 5.5 has introduced developers to another new syntax keyword, actors. When developers use actor instead of class, they effectively isolate the data within their types so that data races can not be introduced to the code and cause app crashes.
One of the major criticisms of concurrent code is that it leads to crash-prone applications. Many developers will say that using the main thread is the best path forward for iOS development. However, the new actor syntax improvement to Swift allows developers to use structured concurrency and avoid data races and strange app crashes.
Final Thoughts
Apple’s WWDC is one of the most highly anticipated and exciting tech events of the year, and the 2021 edition of this event did not disappoint. iOS developers will be excited by the release of Swift 5.5, which offers more robust concurrency features.
While the latest Swift code improvements were exciting to hear about, they were not the only announcements and innovations Apple had for the tech community. Parsing through all of the technical information released by Apple at this year’s WWDC is a tall task. If you want to get clarification on how the new Swift improvements will affect your iOS app, reach out to an app development partner.
Keeping up with all of the latest innovations and releases can be difficult when you are running a business. An app development partner can offer you their experience, expertise, and valuable guidance through all of the new releases and technologies that enter the market. Plus, they can help you leverage new technologies to drive your business forward and reach your goals.