Combined use @mainActor
and propertyWrapper
is not safe? The code will execute directly in the background thread. It is a bug?
Here is the demo:
struct DetailView: View {
@MainActor
@Environment(\.dismiss)
private var dismiss
var body: some View {
Text("Hello")
.task {
await asyncWork()
}
}
private func asyncWork() async {
Thread.sleep(forTimeInterval: 1)
// Crash. Because of not in main Thread.
await dismiss()
}
}