I am trying to add an async do/catch/defer action to a UIButton. However, if I just call a method in the defer block, I get Call to main actor-isolated instance method XXX in a synchronous nonisolated context
error. The workaround I found is to wrap it in another Task
block, like the following. Just want to check if this is the correct way to do it? It'd be good if someone can explain what that error message actually says.
@objc private func post(_ sender: UIButton) {
Task {
// defer { dismiss(animated: true) } -- Doesn't work
defer { Task { await dismiss(animated: true) } }
do {
try await doSomethingAsync()
} catch {
print(error)
}
}
}