Swift 6迁移时闭包作为‘sending’参数传递引发的数据竞争警告问题及修复咨询
Swift 6迁移时闭包作为‘sending’参数传递引发的数据竞争警告问题及修复咨询
嘿,我在迁移到Swift 6的过程中碰到了一个棘手的问题,目前还没找到解决思路。
收到的警告内容如下:
Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure; this is an error in the Swift 6 language mode
我提炼出的示例代码是这样的:
@Observable class Helloworld { var hello: Bool = false func world() { Task { // Compiler warning: "Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure; this is an error in the Swift 6 language mode" await self.work() } } private func work() async { do { try await Task.sleep(for: .seconds(3)) hello = true } catch { // Gotcha } } }
有没有大佬能告诉我该怎么修复这个问题呀?
备注:内容来源于stack exchange,提问作者Jonny
相关产品推荐
相关产品推荐

