Created
September 26, 2016 05:55
-
-
Save sgr-ksmt/4880c5df5aeec9e558622cd6d5b477cb to your computer and use it in GitHub Desktop.
Safe DispatchQueue.main.sync (Swift3.0)
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 { | |
class func mainSyncSafe(execute work: () -> Void) { | |
if Thread.isMainThread { | |
work() | |
} else { | |
DispatchQueue.main.sync(execute: work) | |
} | |
} | |
class func mainSyncSafe<T>(execute work: () throws -> T) rethrows -> T { | |
if Thread.isMainThread { | |
return try work() | |
} else { | |
return try DispatchQueue.main.sync(execute: work) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!
Did you know that you can do stuff like this?
I modified your gist to cater for that. Here's mine: