Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created April 12, 2025 19:12
Show Gist options
  • Save hmlongco/e1e6df5780339b7df293599e9e2b4a6e to your computer and use it in GitHub Desktop.
Save hmlongco/e1e6df5780339b7df293599e9e2b4a6e to your computer and use it in GitHub Desktop.
@MainActor
class Test {
// can do this
func someTask1() {
let _ = ""
Task {
let _ = ""
let data = await doBackgroundTask()
let _ = data // Update UI with data
}
}
// this is better
func someTask2() async {
let _ = ""
let data = await doBackgroundTask()
let _ = data // Update UI with data
}
// mark function as nonisolated
nonisolated func doBackgroundTask() async -> String {
// Heavy work here
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment