1

合用又不安全@mainActorpropertyWrapper代码将直接在后台线程中执行。这是一个错误?

这是演示:

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()
    }

}
4

0 回答 0