如何解决转义闭包捕获变异的“自我”参数SWIFT 5
我正在尝试使用@propertyWrapper创建线程安全的结构,但是在设置值时,我在操场上遇到了这样的错误。
仅当我将async
更改为sync
时才出错,但是我只需要具有async
功能
@propertyWrapper
struct SafeThread<Value>{
private let queue = DispatchQueue(label: "sellQueue",attributes:.concurrent )
private var value: Value
init (wrappedValue: Value){
self.value = wrappedValue;
}
var wrappedValue :Value {
get { queue.sync { value } }
set { queue.async(flags: .barrier) { self.value = newValue } } // there an error
}
}
我想在哪里使用它:
class Safe{
@SafeThread var foo = 0;
func threadSafetyExperiment() {
DispatchQueue.global().sync {
DispatchQueue.concurrentPerform(iterations: 1_000) { _ in
self.foo += 1
}
print(self.foo)
}
}
}
解决方法
您需要使用nonmutating set
并为此使用外部存储来存储包装的值。
这是可行的方法。在Xcode 12 / iOS 14上进行了测试。
@propertyWrapper
struct SafeThread<Value>{
private class Storage {
var value: Value
init(initialValue: Value) {
value = initialValue
}
}
private let queue = DispatchQueue(label: "sellQueue",attributes:.concurrent )
private var storage: Storage
init (wrappedValue: Value){
self.storage = Storage(initialValue: wrappedValue);
}
var wrappedValue :Value {
get { queue.sync { self.storage.value } }
nonmutating set { queue.async(flags: .barrier) { self.storage.value = newValue } }
}
}
,
这是一个重复的问题。只需将SafeThread
设为一个班级即可。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。