Created
April 19, 2021 11:25
-
-
Save srstanic/862b68cb13bc7996ef7c41a22590ec30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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