SwiftUI: Difference between async{} and Task.init{ }

Viewed 92

I am using Xcode 13.4 and trying to launch an async function when the Picker changes.

Picker(NSLocalizedString("Please choose a project", comment: ""), selection: $selectedProjectKey) {
    ForEach(projectKeys, id: \.self) {
        Text($0)
    }
}
.font(.system(size: 14))
.onChange(of: selectedProjectKey, perform: { (selectedKey) in
    print("selected key \(selectedKey)")
    async {
        do {
            let projectTasks = try await api.getProjectTasksByProjectKey(projectKey: selectedKey)
            projectTasksKeys = projectTasks.map{$0.key}
        } catch {
            /// To define error behavour
        }
        
    }
})

It works, but I know that async is deprecated (yellow warning). That's why, I tried with Task.init {... } or Task { ... } and instead, I get an error:

"Trailing closure passed to parameter of type 'Decoder' that does not accept a closure".

I suppose that I did something definitely wrong but I can't understand what it is and what difference there is between async{...} and Task.init { ... } .

0 Answers
Related