Last active
July 29, 2021 05:32
-
-
Save marktrobinson/45d98dc554dcbc2892efd92c81502263 to your computer and use it in GitHub Desktop.
This file contains 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
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