Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 491 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dispatch_once after the Swift 3 GCD API changes

#11
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)
}
}
Reply

#12
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.
Reply

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

}


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through