Created
August 16, 2016 19:12
-
-
Save jonathanduty/c3055e0c660bb4da36cee9f98377784f to your computer and use it in GitHub Desktop.
Neato OnMain swift keyword.
This file contains hidden or 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
import Foundation | |
func onMain(closure: () -> ()) { | |
if NSThread.isMainThread() { | |
closure() | |
} | |
else { | |
dispatch_async(dispatch_get_main_queue(), closure) | |
} | |
} | |
func delay(delay: NSTimeInterval, closure: () -> Void) { | |
dispatch_after( | |
dispatch_time( | |
DISPATCH_TIME_NOW, | |
Int64(delay * Double(NSEC_PER_SEC)) | |
), | |
dispatch_get_main_queue(), closure) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift 3:
func onMain(closure: @escaping () -> ()) {
if Thread.isMainThread {
closure()
}
else {
DispatchQueue.main.async(execute: closure)
}
}