Hello -
I've been experimenting with async
/await
and Task
now that they're available for iOS 14 and as part of my experimenting I've been trying to get the following code working
import SwiftUI
struct ContentView: View {
@State var foo: Int
var body: some View {
if foo == 0 {
Text("Hello, world!")
} else {
Text("Goodbye!")
}
}
init() {
foo = 0
setup()
}
func setup() {
Task {
try? await Task.sleep(nanoseconds: 3_000_000_000)
await MainActor.run { foo = 1 }
}
}
}
When I run it in the simulator the text never switches over to "Goodbye!". The code compiles without any errors and using break points I can see that all the code runs as expected but the text doesn't update. If I change the code to the following
struct ContentView: View {
@State var foo: Int
var body: some View {
Group {
if foo == 0 {
Text("Hello, world!")
} else {
Text("Goodbye!")
}
}
.task {
try? await Task.sleep(nanoseconds: 3_000_000_000)
foo = 1
}
}
init() {
foo = 0
}
}
The code works as expected and "Hello, world!" switches to "Goodbye!". Unfortunately the .task
modifier is only available for iOS 15 and the code I'm working with needs to be compatible with iOS 14.
I'd love to understand why the first example doesn't work and if there is a way to make it work without having to create an ObservableObject
to hold foo
Thanks in advance - AYAL