123456789101112131415161718192021222324252627282930313233 |
- //
- // DispatchQueue+Extension.swift
- // O2Platform
- //
- // Created by FancyLou on 2019/10/23.
- // Copyright © 2019 zoneland. All rights reserved.
- //
- import Foundation
- 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
- */
- class func once(token: String, block:()->Void) {
- objc_sync_enter(self); defer { objc_sync_exit(self) }
-
- if _onceTracker.contains(token) {
- return
- }
-
- _onceTracker.append(token)
- block()
- }
- }
|