Last active
August 14, 2019 15:06
-
-
Save Exey/865d90285f10337ed2b3940ad227f1b4 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
@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