How can I use async/await with SwiftUI in Swift 5.5?

I’m the author of the article you referenced.

As discussed in Discover concurrency in SwiftUI, views can make use of the new .task { } and .refreshable { } modifiers to fetch data asynchronously.

So you now have the following options to call your async code:

func someSyncMethod() {
  doSomeSyncWork()
  Task {
    await methodThatIsAsync()
  }
}
List {
}
.task {
  await methodThatIsAsync()
}
List {
}
.refreshable {
  await methodThatIsAsync()
}

If you’re using a separate view model, make sure to mark it as @MainActor to ensure property updates get executed on the main actor.

I updated the code for my article: https://github.com/peterfriese/Swift-Async-Await-Experiments

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)