Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Created October 12, 2018 11:26
Show Gist options
  • Save nanoxd/138d3b579ae87af1c03184debb062561 to your computer and use it in GitHub Desktop.
Save nanoxd/138d3b579ae87af1c03184debb062561 to your computer and use it in GitHub Desktop.
[DispatchQueue+isMain] Determine if the current GCD Queue is the main queue #swift
extension DispatchQueue {
fileprivate static let mainQueueKey = DispatchSpecificKey<()>()
static func configureMainQueue() {
main.setSpecific(key: mainQueueKey, value: ())
}
}
public extension DispatchQueue {
/// Easy and safe way of checking if the current queue is the main queue
static var isMain: Bool {
return getSpecific(key: mainQueueKey) != nil
}
/// Easy and safe way of checking if the current queue is the main queue
var isMain: Bool {
return getSpecific(key: DispatchQueue.mainQueueKey) != nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment