swift - Swift 3 GCD API 更改后的 dispatch_once

标签 swift grand-central-dispatch

在语言版本 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/

相关文章:

objective-c - 为什么我们不能在当前队列上使用 dispatch_sync?

ios - 如何为 VoiceOver 排队多个辅助功能通知?

swift - Swinject - 对成员的模糊引用

ios - iCarousel 上的 carouselCurrentItemIndexDidChange 仅在我滑动时被调用

ios - 跟踪 _objc_msgSend 在 dispatch_sync block 内崩溃的策略

ios - 'dispatch_get_main_queue()' 的使用不明确

ios - 单击按钮后 UILabel 不更新

ios - 如何使用 Swift 调试我的 iOS 应用程序

ios - 在 iOS/Mac 上通过 GCD 实现多线程

ios - 这个 dispatch_once 单例可以返回 nil 吗?