Skip to content

Instantly share code, notes, and snippets.

@loganwright
Created May 21, 2015 18:01
Show Gist options
  • Save loganwright/39056d582d3a28eb6b32 to your computer and use it in GitHub Desktop.
Save loganwright/39056d582d3a28eb6b32 to your computer and use it in GitHub Desktop.
Dispatching events to later in Swift.
func After(after: NSTimeInterval, op: () -> ()) {
After(after, op, nil)
}
func After(after: NSTimeInterval, op: () -> (), completion: (() -> Void)?) {
let seconds = Int64(after * Double(NSEC_PER_SEC))
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, seconds)
dispatch_after(dispatchTime, dispatch_get_main_queue()) {
let blockOp = NSBlockOperation(block: op)
blockOp.completionBlock = completion
NSOperationQueue.mainQueue().addOperation(blockOp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment