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

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

dispatch_once報錯.png

官方推薦的解決辦法:

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()
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容