Created
December 20, 2016 01:10
-
-
Save asaday/804fde35d1e6915c24d8fd48cbcd68c7 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
public struct Dispatch { | |
public static func main(block: @escaping ()->()) { | |
return DispatchQueue.main.async(execute: block) | |
} | |
public static func background(block: @escaping ()->()) { | |
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: block) | |
} | |
public static func main(after seconds: Double, block: @escaping ()->()) { | |
return DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: block) | |
} | |
public static func background(after seconds: Double, block: @escaping ()->()) { | |
return DispatchQueue.global(qos: DispatchQoS.QoSClass.background).asyncAfter(deadline: DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: block) | |
} | |
// use in main-thread, call this or ...ASync in async{} | |
public static func await<T>(_ block: @escaping () -> T?) -> T? { | |
var result: T? | |
var done = false | |
background { | |
result = block() | |
done = true | |
} | |
while done == false { CFRunLoopRunInMode(CFRunLoopMode.defaultMode, 0.02, false) } | |
return result | |
} | |
public static func async(_ block: @escaping () -> Void) { | |
CFRunLoopPerformBlock(CFRunLoopGetCurrent(), CFRunLoopMode.commonModes as CFTypeRef!, block) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment