Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/b05c4559493d38705583eb172af5cff3 to your computer and use it in GitHub Desktop.
Save jacobsapps/b05c4559493d38705583eb172af5cff3 to your computer and use it in GitHub Desktop.
// All code runs on MainActor
@MainActor
func updateUI(with text: String) {
label.text = text
}
// properties and functions implicitly marked @MainActor
@MainActor
class ViewModel: ObservableObject {
// ...
}
// Use MainActor in async contexts
Task { @MainActor in
let data = await getData() // await can run off main actor
self.data = data // returns to main actor
}
// safely jump from async context to main actor
await MainActor.run {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment