简体   繁体   English

转换不可发送的 function 值可能会引入数据竞争

[英]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.

相关问题 如何解决 - UndoManager 不可发送的编译器警告 - How to resolve - compiler warning that UndoManager is non-sendable 将非转义值转换为 'Any' 可能允许它转义错误 - 在非转义函数内 - Converting non-escaping value to 'Any' may allow it to escape error - within not escaping function 如何修复 escaping 关闭? 错误是:转换非转义值可能允许它转义 - How to fix escaping closure? Error is: Converting non-escaping value may allow it to escape Xcode 10 Swift构建错误:“将非转义的值转换为'T'可能会使其逸出” - Xcode 10 Swift build error: “Converting non-escaping value to 'T' may allow it to escape” 无法调用非功能类型'(((UInt)-> Data?)!' 与Alamofire 4 - Cannot call value of non-function type '((UInt) -> Data?)!' with Alamofire 4 从 firebase 调用数据时,void 函数中出现意外的非空返回值 - Unexpected non-void return value in void function while calling data from firebase Swift 3 [String:AnyObject]()给出无法调用非函数类型UInt的值->数据 - Swift 3 [String: AnyObject]() gives Cannot call value of non-function type UInt -> Data 尝试检索提供程序的数据时,如何解决“无法调用非函数类型[UserInfo]的值”的问题? - How to fix “Cannot call value of non-function type [UserInfo]” while trying to retrieve provider's data? 非空函数应该返回一个值 - Non-void function should return a value 无法调用非函数类型SKShapeNode的值 - Cannot call value of non function type SKShapeNode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM