Created
April 12, 2025 19:12
-
-
Save hmlongco/e1e6df5780339b7df293599e9e2b4a6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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