在语言版本 3 发生变化后,Swift 中 dispatch_once
的新语法是什么?旧版本如下。
var token: dispatch_once_t = 0
func test() {
dispatch_once(&token) {
}
}
这些 are the changes to libdispatch那是做的。
最佳答案
虽然使用延迟初始化的全局变量对某些一次性初始化有意义,但对其他类型没有意义。对于像单例这样的事情使用惰性初始化全局变量很有意义,对于像保护 swizzle 设置这样的事情没有多大意义。
这是 dispatch_once 的 Swift 3 风格实现:
public extension DispatchQueue {
private static var _onceTracker = [String]()
/**
Executes a block of code, associated with a unique token, only once. The code is thread safe and will
only execute the code once even in the presence of multithreaded calls.
- parameter token: A unique reverse DNS style name such as com.vectorform.<name> or a GUID
- parameter block: Block to execute once
*/
public class func once(token: String, block:@noescape(Void)->Void) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
if _onceTracker.contains(token) {
return
}
_onceTracker.append(token)
block()
}
}
这是一个用法示例:
DispatchQueue.once(token: "com.vectorform.test") {
print( "Do This Once!" )
}
或使用 UUID
private let _onceToken = NSUUID().uuidString
DispatchQueue.once(token: _onceToken) {
print( "Do This Once!" )
}
由于我们目前正处于从 swift 2 到 3 的过渡时期,下面是一个 swift 2 实现示例:
public class Dispatch
{
private static var _onceTokenTracker = [String]()
/**
Executes a block of code, associated with a unique token, only once. The code is thread safe and will
only execute the code once even in the presence of multithreaded calls.
- parameter token: A unique reverse DNS style name such as com.vectorform.<name> or a GUID
- parameter block: Block to execute once
*/
public class func once(token token: String, @noescape block:dispatch_block_t) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
if _onceTokenTracker.contains(token) {
return
}
_onceTokenTracker.append(token)
block()
}
}
关于swift - Swift 3 GCD API 更改后的 dispatch_once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37886994/