Skip to content

Instantly share code, notes, and snippets.

@Exey
Last active August 14, 2019 15:06
Show Gist options
  • Save Exey/865d90285f10337ed2b3940ad227f1b4 to your computer and use it in GitHub Desktop.
Save Exey/865d90285f10337ed2b3940ad227f1b4 to your computer and use it in GitHub Desktop.
@inlinable func trampolineToMain(_ function: @escaping @autoclosure ()->()) -> Bool {
if Thread.isMainThread {
return false
} else {
DispatchQueue.main.async {
function()
}
return true
}
}
// Use
func doSmthWithUI(a:Int, b:String) {
print("BEFORE \(Thread.isMainThread)")
if trampolineToMain(self.doSmthWithUI(a: a, b: b)) {
return
}
print("AFTER \(Thread.isMainThread) a=\(a) b=\(b)")
}
DispatchQueue.global(qos: .background).async {
doSmthWithUI(a: 42, b: "I love Swift")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment