Created
August 26, 2021 11:45
-
-
Save dimabiserov/093e7630749141425834afa4e283457f to your computer and use it in GitHub Desktop.
Dispatch Extensions
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 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