Skip to content

Instantly share code, notes, and snippets.

@jonahaung
Last active March 20, 2020 16:02
Show Gist options
  • Save jonahaung/ed4c2f83bc8a650538b0f74421b042b8 to your computer and use it in GitHub Desktop.
Save jonahaung/ed4c2f83bc8a650538b0f74421b042b8 to your computer and use it in GitHub Desktop.
This method will dispatch the `block` to self. If `self` is the main queue, and current thread is main thread, the block. will be invoked immediately instead of being dispatched.
extension DispatchQueue {
func safeAsync(_ block: @escaping ()->()) {
if self === DispatchQueue.main && Thread.isMainThread {
block()
} else {
async { block() }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment