Last active
March 20, 2020 16:02
-
-
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.
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
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