WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to make function parameters isolated

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Any properties and methods that belong to an actor are isolated to that actor, but you can make external functions isolated to an actor if you want. This allows the function to access actor-isolated state as if it were inside that actor, without needing to use await.

Here’s a simple example so you can see what I mean:

actor DataStore {
    var username = "Anonymous"
    var friends = [String]()
    var highScores = [Int]()
    var favorites = Set<Int>()

    init() {
        // load data here

    func save() {
        // save data here

func debugLog(dataStore: isolated DataStore) {
    print("Username: \(dataStore.username)")
    print("Friends: \(dataStore.friends)")
    print("High scores: \(dataStore.highScores)")
    print("Favorites: \(dataStore.favorites)")

let data = DataStore()
await debugLog(dataStore: data)

Download this as an Xcode project

That creates a DataStore actor with various properties plus a couple of placeholder methods, then creates a debugLog() method that prints those without using await – they can be accessed directly. Notice the addition of the isolated keyword in the function signature; that’s what allows this direct access, and it even allows the function to write to those properties too.

Using isolated like this does not bypass any of the underlying safety or implementation of actors – there can still only be one thread accessing the actor at any one time. What we’ve done just pushes that access out by a level, because now the whole function must be run on that actor rather than just individual lines inside it. In practice, this means debugLog(dataStore:) needs to be called using await.

This approach has an important side effect: because the whole function is now isolated to the actor, it must be called using await even though it isn’t marked as async. This makes the function itself a single potential suspension point rather than individual accesses to the actor being suspension points.

In case you were wondering, you can’t have two isolation parameters, because it wouldn’t really make sense – which one is executing the function?

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Similar solutions…

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.