how to make shared propery async

I was watching https://developer.apple.com/videos/play/wwdc2023/10248/ , in this video it is adviced to make below shated property async to benefit from concurency (in video 40:04 , exact time) , do yo know how to do it ?

class ColorizingService { static let shared = ColorizingService()

func colorize(_ grayscaleImage: CGImage) async throws -> CGImage
// [...]

}

struct ImageTile: View { // [...]

// implicit @MainActor
var body: some View {
    mainContent
        .task() { // inherits @MainActor isolation
            // [...]
            result = try await ColorizingService.shared.colorize(image)
        }
}

}

Replies

Earlier in the video there is an example of how to make a property async. In that case the property is already a computed property, so it's easy to add the async keyword here. So in this case you first need to make the shared property a computed property, such that you can write it as get async.

You can do this by having the actual stored singleton be a private let property, and then exposing it as public async var property. So from this:

class ColorizingService { 
    static let shared = ColorizingService()
}

you go to this

class ColorizingService { 
    private static let _sharedStorage = ColorizingService()
    public var shared: ColorizingService {
        get async {
            return shared
        }
    }
}