Skip to content

Instantly share code, notes, and snippets.

@marktrobinson
Last active July 29, 2021 05:32
Show Gist options
  • Save marktrobinson/45d98dc554dcbc2892efd92c81502263 to your computer and use it in GitHub Desktop.
Save marktrobinson/45d98dc554dcbc2892efd92c81502263 to your computer and use it in GitHub Desktop.
import SwiftUI
@main
struct AsyncTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(State())
}
}
}
class State: ObservableObject {
@Published var counter: Int = 0
func increase() async {
_ = Task {
counter += 1
}
}
}
struct ContentView: View {
@EnvironmentObject var state: State
var body: some View {
VStack {
Text(String(state.counter))
Button("Increase") {
Task {
await state.increase()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(State())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment