Skip to content

Instantly share code, notes, and snippets.

@dimabiserov
Created August 26, 2021 11:45
Show Gist options
  • Save dimabiserov/093e7630749141425834afa4e283457f to your computer and use it in GitHub Desktop.
Save dimabiserov/093e7630749141425834afa4e283457f to your computer and use it in GitHub Desktop.
Dispatch Extensions
import Foundation
func MainAsync(task: @escaping ()->()) {
DispatchQueue.main.async {
task()
}
}
func Background(task:@escaping () throws -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
do {
try task()
} catch let error as NSError {
print("error in background thread:\(error.localizedDescription)")
}
}
}
func DelayTask(_ timeInteval: TimeInterval, _ task:@escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: (.now() + timeInteval)) {
task()
}
}
//Fast use anywhere with autocomplete
DelayTask(1) {
print("hello")
}
Background {
print("background")
}
MainAsync {
print("Main")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment