Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Some functions inside `Task` closure becomes an async function in Swift

I’m not sure how is this being triggered, but sometimes when I call a function inside Task { } XCode gives an error that instructs me to add await in the called function.

Here’s a simplified instance I experienced:

public class Loader: UIView {
   

   
    public class func show(animated: Bool = true) {
        // showing the loader code
    }
}

enum Utilities {
    func doSomeAsyncTask(action: @escaping ()async->()) {
        Task {
            Loader.show() // XCode error: "Expression is 'async' but is not marked with 'await'"
            action()
        }      
    }
}

XCode shows the function is an async one

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does this have something to do that this is a class or something?
enter image description here

>Solution :

UIView is marked with the global actor @MainActor. Since Loader inherits from UIView, Loader is implicitly marked with @MainActor too.

This means that all the methods in Loader are isolated to the MainActor. The things in Task { ... } are not isolated to the MainActor. Therefore, to run a MainActor-isolated method in Task { ... }, you need to await for it. MainActor could be busy with something else at that time, and you’d need to wait for a bit before it runs show.

The methods that do not become async when you put them in Task { ... } are probably not isolated to a particular actor, e.g.

// not actor-isolated
func square(_ x: Int) -> Int { x * x }

enum Utilities {
    func doSomeAsyncTask(action: @escaping ()async->()) {
        Task {
            print(square(4)) // doesn't need async
            await action()
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading