当我用@MainAct或标记一个带有异步函数的协议时,它的一致性没有指定它的函数是异步的,它不在主线程上执行.我不知道这是不是一个窃听器.
例如,给定以下协议和实施:
@MainAct或
protocol MyMainAct或Protocol {
func doStuff() async
}
struct MyMainAct或Struct: MyMainAct或Protocol {
nonisolated init() {}
func doStuff() {
print(">>> \(Thread.isMainThread)")
}
}
当我执行此代码时:
Task {
let dependency: MyMainAct或Protocol = MyMainAct或Struct()
print(">>> \(Thread.isMainThread)")
await dependency.doStuff()
}
这两个打印语句都在后台线程中运行.
但通过以下两种方式的调整
@MainAct或
protocol MyMainAct或Protocol {
func doStuff()
}
struct MyMainAct或Struct: MyMainAct或Protocol {
nonisolated init() {}
func doStuff() {
print(">>> \(Thread.isMainThread)")
}
}
或
@MainAct或
protocol MyMainAct或Protocol {
func doStuff() async
}
struct MyMainAct或Struct: MyMainAct或Protocol {
nonisolated init() {}
func doStuff() async {
print(">>> \(Thread.isMainThread)")
}
}
I get the c或rect result, where the body of the function inside the struct runs in the main thread.
Is this expected behavi或? What is the explanation behind it.