Concurrency
Dive deep into Swift’s latest concurrency changes, covering async-await, actors, and more.
MainActor usage in Swift explained to dispatch to the main thread
MainActor is a new attribute introduced in Swift 5.5 as a global actor providing an executor that performs its tasks on the main thread. When building apps, it's essential to perform UI updating tasks on the main thread, which can sometimes be challenging when using several background threads. Using the ...
How to Use URLSession with Async/Await for Network Requests in Swift
URLSession allows you to perform network requests and becomes even more powerful with its async/await APIs. You can request data from a given URL and parse it into a decoded structure before displaying its data in a view. Popular frameworks like Alamofire aim to make it easier to perform requests, ...
Async await in Swift explained with code examples
Async await is part of the new structured concurrency changes that arrived in Swift 5.5 during WWDC 2021. Concurrency in Swift means allowing multiple pieces of code to run at the same time. This is a very simplified description, but it should give you an idea already how important concurrency ...
Create a complete design system using SwiftUIGet a step-by-step blueprint for creating a complete design system using SwiftUI. From Figma to fully functional code, it’s all included in the Crafting Consistency book.
Grab your copy today with 40% off.
Swift 6: Incrementally migrate your Xcode projects and packages
Apple announced Swift 6 during WWDC 2024 as a major release of their programming language. It aims to create a fantastic development experience, and many of the latest features we know today are part of the road toward this major version bump. The Swift development team shared their focus areas ...
Concurrency-safe global variables to prevent data races
Concurrency-safe global variables help you prevent data races and allow you to solve strict-concurrency-related warnings. Since you can access global variables from any context, ensuring access is safe by removing mutability or conforming to Sendable is essential. As a developer, you must prevent data races since they can make your ...
Unit testing async/await Swift code
Unit tests allow you to validate code written using the latest concurrency framework and async/await. While writing tests doesn't differ much from synchronous tests, there are a few crucial concepts to be aware of when validating asynchronous code. If you're new to async/await, I encourage you first to read Async ...
Thread dispatching and Actors: understanding execution
Actors ensure your code is executed on a specific thread, like the main or a background thread. They help you synchronize access to mutable states and prevent data races. However, developers commonly misunderstand how actors dispatch to threads in non-async contexts. It's an essential understanding to avoid unexpected crashes. Before ...
@preconcurrency: Incremental migration to concurrency checking
The @preconcurrency attribute is part of the tools that help you incrementally migrate to strict concurrency checking. When async/await was introduced by Apple, we were writing non-structured asynchronous code, mainly using closures. On our road to Swift 6, we must prepare our projects for strict concurrency checks done by the ...
Detached Tasks in Swift explained with code examples
Detached tasks allow you to create a new top-level task and disconnect from the current structured concurrency context. You could argue that using them results in unstructured concurrency since you're disconnecting potentially relevant tasks. While it sounds terrible to disconnect from structured concurrency, there are still examples of use cases ...
Task Groups in Swift explained with code examples
Task Groups in Swift allow you to combine multiple parallel tasks and wait for the result to return when all tasks are finished. They are commonly used for tasks like combining multiple API request responses into a single response object. Read my article about tasks first if you're new to ...
Sendable and @Sendable closures explained with code examples
Sendable and @Sendable are part of the concurrency changes that arrived in Swift 5.5 and address a challenging problem of type-checking values passed between structured concurrency constructs and actor messages. Before diving into the topic of sendables, I encourage you to read up on my articles around async/await, actors, and ...