合用又不安全@mainActor
?propertyWrapper
代码将直接在后台线程中执行。这是一个错误?
这是演示:
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()
}
}