在 Swift 3 GCD API 更改之后进行调度

经过语言版本3的修改后,Swift 中 dispatch_once的新语法是什么?旧版本如下。

var token: dispatch_once_t = 0
func test() {
dispatch_once(&token) {
}
}

这些 是对《自由派遣》的修改

56550 次浏览

From the doc:

Dispatch
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.

While using lazy initialized globals can make sense for some one time initialization, it doesn't make sense for other types. It makes a lot of sense to use lazy initialized globals for things like singletons, it doesn't make a lot of sense for things like guarding a swizzle setup.

Here is a Swift 3 style implementation of dispatch_once:

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()
}
}

Here is an example usage:

DispatchQueue.once(token: "com.vectorform.test") {
print( "Do This Once!" )
}

or using a UUID

private let _onceToken = NSUUID().uuidString


DispatchQueue.once(token: _onceToken) {
print( "Do This Once!" )
}

As we are currently in a time of transition from swift 2 to 3, here is an example swift 2 implementation:

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()
}


}

Expanding on Tod Cunningham's answer above, I've added another method which makes the token automatically from file, function, and line.

public extension DispatchQueue {
private static var _onceTracker = [String]()
    

public class func once(
file: String = #file,
function: String = #function,
line: Int = #line,
block: () -> Void
) {
let token = "\(file):\(function):\(line)"
once(token: token, block: block)
}
    

/**
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: () -> Void
) {
objc_sync_enter(self)
defer { objc_sync_exit(self) }
        

guard !_onceTracker.contains(token) else { return }
        

_onceTracker.append(token)
block()
}
}

So it can be simpler to call:

DispatchQueue.once {
setupUI()
}

and you can still specify a token if you wish:

DispatchQueue.once(token: "com.hostname.project") {
setupUI()
}

I suppose you could get a collision if you have the same file in two modules. Too bad there isn't #module

You can still use it if you add a bridging header:

typedef dispatch_once_t mxcl_dispatch_once_t;
void mxcl_dispatch_once(mxcl_dispatch_once_t *predicate, dispatch_block_t block);

Then in a .m somewhere:

void mxcl_dispatch_once(mxcl_dispatch_once_t *predicate, dispatch_block_t block) {
dispatch_once(predicate, block);
}

You should now be able to use mxcl_dispatch_once from Swift.

Mostly you should use what Apple suggest instead, but I had some legitimate uses where I needed to dispatch_once with a single token in two functions and there is not covered by what Apple provide instead.

Use the class constant approach if you are using Swift 1.2 or above and the nested struct approach if you need to support earlier versions. An exploration of the Singleton pattern in Swift. All approaches below support lazy initialization and thread safety. dispatch_once approach is not worked in Swift 3.0

Approach A: Class constant

class SingletonA {


static let sharedInstance = SingletonA()


init() {
println("AAA");
}


}

Approach B: Nested struct

class SingletonB {


class var sharedInstance: SingletonB {
struct Static {
static let instance: SingletonB = SingletonB()
}
return Static.instance
}


}

Approach C: dispatch_once

class SingletonC {


class var sharedInstance: SingletonC {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: SingletonC? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = SingletonC()
}
return Static.instance!
}
}

Swift 3: For those who likes reusable classes (or structures):

public final class /* struct */ DispatchOnce {
private var lock: OSSpinLock = OS_SPINLOCK_INIT
private var isInitialized = false
public /* mutating */ func perform(block: (Void) -> Void) {
OSSpinLockLock(&lock)
if !isInitialized {
block()
isInitialized = true
}
OSSpinLockUnlock(&lock)
}
}

Usage:

class MyViewController: UIViewController {


private let /* var */ setUpOnce = DispatchOnce()


override func viewWillAppear() {
super.viewWillAppear()
setUpOnce.perform {
// Do some work here
// ...
}
}


}

Update (28 April 2017): OSSpinLock replaced with os_unfair_lock due deprecation warnings in macOS SDK 10.12.

public final class /* struct */ DispatchOnce {
private var lock = os_unfair_lock()
private var isInitialized = false
public /* mutating */ func perform(block: (Void) -> Void) {
os_unfair_lock_lock(&lock)
if !isInitialized {
block()
isInitialized = true
}
os_unfair_lock_unlock(&lock)
}
}

Edit

@Frizlab's answer - this solution is not guaranteed to be thread-safe. An alternative should be used if this is crucial

Simple solution is

lazy var dispatchOnce : Void  = { // or anyName I choose


self.title = "Hello Lazy Guy"


return
}()

used like

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
_ = dispatchOnce
}

You can declare a top-level variable function like this:

private var doOnce: ()->() = {
/* do some work only once per instance */
return {}
}()

then call this anywhere:

doOnce()

I improve above answers get result:

import Foundation
extension DispatchQueue {
private static var _onceTracker = [AnyHashable]()


///only excute once in same file&&func&&line
public class func onceInLocation(file: String = #file,
function: String = #function,
line: Int = #line,
block: () -> Void) {
let token = "\(file):\(function):\(line)"
once(token: token, block: block)
}


///only excute once in same Variable
public class func onceInVariable(variable:NSObject, block: () -> Void){
once(token: variable.rawPointer, block: block)
}
/**
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: AnyHashable,block: () -> Void) {
objc_sync_enter(self)
defer { objc_sync_exit(self) }


guard !_onceTracker.contains(token) else { return }


_onceTracker.append(token)
block()
}


}


extension NSObject {
public var rawPointer:UnsafeMutableRawPointer? {
get {
Unmanaged.passUnretained(self).toOpaque()
}
}
}
import UIKit


// dispatch once
class StaticOnceTest {
    

static let test2 = {
print("Test " + $0 + " \($1)")
}("mediaHSL", 5)
    

lazy var closure: () = {
test(entryPoint: $0, videos: $1)
}("see all" , 4)
    

private func test(entryPoint: String, videos: Int) {
print("Test " + entryPoint + " \(videos)")
}
}


print("Test-1")
let a = StaticOnceTest()
a.closure
a.closure
a.closure
a.closure
StaticOnceTest.test2
StaticOnceTest.test2
StaticOnceTest.test2
StaticOnceTest.test2

OUTPUT:

Test-1
Test see all 4
Test mediaHSL 5

You can use a lazy var closure and execute it immediately with (#arguments_if_needed) so that it will call only one time. You can call any instance function inside of the closure [advantage].

You can pass multiple arguments based on need. You can capture those arguments when the class has been initialised and use them.

Another option: You can use a static let closure and it will execute only one time but you cannot call any instance func inside that static let clsoure. [disadvantage]

thanks!

Swift 5

dispatch_once is still available in libswiftFoundation.dylib standard library which is embedded to any swift app so you can access to exported symbols dynamically, get the function's symbol pointer, cast and call:

import Darwin


typealias DispatchOnce = @convention(c) (
_ predicate: UnsafePointer<UInt>?,
_ block: () -> Void
) -> Void


func dispatchOnce(_ predicate: UnsafePointer<UInt>?, _ block: () -> Void) {
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
    

if let sym = dlsym(RTLD_DEFAULT, "dispatch_once") {
let f = unsafeBitCast(sym, to: DispatchOnce.self)
f(predicate, block)
}
else {
fatalError("Symbol not found")
}
}

Example:

var token: UInt = 0


for i in 0...10 {
print("iteration: \(i)")
    

dispatchOnce(&token) {
print("This is printed only on the first call")
}
}

Outputs:

iteration: 0
This is printed only on the first call
iteration: 1
iteration: 2
iteration: 3
iteration: 4
iteration: 5
iteration: 6
iteration: 7
iteration: 8
iteration: 9
iteration: 10