Swift-沒有dispatch_once實現只調用一次

早在Swift 3的時候,dispatch_once就被蘋果廢棄了,並且推薦使用懶初始化全局變量方案代替。


官方推薦的解決辦法:

The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once provided. Example:

let myGlobal = { … global contains initialization in a call to a closure … }()

_ = myGlobal  // using myGlobal will invoke the initialization code only the first time it is used.

下面來介紹4種實現dispatch_once的只調用一次的方法

一. 帶立即執行閉包初始化器的全局變量
let dispatchOnce: () = {
    print("dispatchOnce-------")
}()

/// 使用Aspects做交換: 用一個全局的常量來實現的dispatchOnce效果
let doSwizzles: () = {
    print("doSwizzles----")
    let oriSel1 = #selector(UIViewController.viewWillAppear(_:))
    let wrappedBlock: @convention(block) (AspectInfo, Bool) -> Void = { aspectInfo, _ in
        print("wrappedBlock---")
    }
    _ = try? UIViewController.aspect_hook(oriSel1, with: AspectOptions.positionBefore, usingBlock: wrappedBlock)

}()

// 之後在合適的時機使用變量,比如:
_ = dispatchOnce
二、全局常量
let foo = ViewController()

三、類,結構,枚舉中的靜態屬性

class Person22 {
    static var dog = Dog()
}
四、封裝一個once函數
public extension DispatchQueue {
    private static var _onceTracker = [String]()
    /// 函數只被執行一次
    static func once(token: String, block: () -> ()) {

       // objc_sync_enter + objc_sync_exit 相當於OC中的@sychronize() {}
        objc_sync_enter(self)
        defer {
            objc_sync_exit(self)
        }
        if _onceTracker.contains(token) {
            return;
        }
        _onceTracker.append(token)
        block()
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章