[英]Converting non-sendable function value may introduce data races
I have a simple piece of code:我有一段简单的代码:
struct ContentView: View {
var body: some View {
Text("Hello world!")
.task {
await myAsyncFunc()
}
}
private func myAsyncFunc() async {}
}
This compiles completely fine.这编译得很好。 However, if I replace the task with this:
但是,如果我用这个替换任务:
.task(myAsyncFunc)
It doesn't work, and gives me the error below:它不起作用,并给我以下错误:
Converting non-sendable function value to '@Sendable () async -> Void' may introduce data races
将不可发送的 function 值转换为“@Sendable () async -> Void”可能会引入数据竞争
Why is this, and how can I fix it?为什么会这样,我该如何解决?
As it states, the function can be marked as @Sendable
.正如它所说, function 可以标记为
@Sendable
。 This is used to prevent data races.这用于防止数据竞争。
Change it to this:将其更改为:
@Sendable private func myAsyncFunc() async {}
Note that the @Sendable
must be before the access modifier.请注意,
@Sendable
必须在访问修饰符之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.