Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created April 19, 2021 11:25
Show Gist options
  • Save srstanic/862b68cb13bc7996ef7c41a22590ec30 to your computer and use it in GitHub Desktop.
Save srstanic/862b68cb13bc7996ef7c41a22590ec30 to your computer and use it in GitHub Desktop.
/// Inspired by
/// http://blog.benjamin-encz.de/post/main-queue-vs-main-thread/
/// and https://stackoverflow.com/a/60348601
/// Associates a predefined key/value pair with the main queue and later uses the presence of it
/// to determine whether the current queue is the main queue.
extension DispatchQueue {
private static let mainQueueCheckKey: DispatchSpecificKey<String> = {
let mainQueueCheckKey = DispatchSpecificKey<String>()
DispatchQueue.main.setSpecific(key: mainQueueCheckKey, value: mainQueueCheckValue)
return mainQueueCheckKey
}()
private static let mainQueueCheckValue = "MainQueueCheck"
public static var isRunningOnMainQueue: Bool {
getSpecific(key: mainQueueCheckKey) == mainQueueCheckValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment