Skip to content

Instantly share code, notes, and snippets.

@loganwright
Last active October 28, 2015 14:24
Show Gist options
  • Save loganwright/4ca3b952a622f2bcb990 to your computer and use it in GitHub Desktop.
Save loganwright/4ca3b952a622f2bcb990 to your computer and use it in GitHub Desktop.
After
func After(after: NSTimeInterval, op: () -> ()) {
After(after, op: op, completion: nil)
}
func After(after: NSTimeInterval, numberOfTimes: Int, op: () -> (), completion: Void -> Void = {}) {
let numberOfTimesLeft = numberOfTimes - 1
let wrappedCompletion: Void -> Void
if numberOfTimesLeft > 0 {
wrappedCompletion = {
After(after, numberOfTimes: numberOfTimesLeft, op: op, completion: completion)
}
} else {
wrappedCompletion = completion
}
After(after, op: op, completion: wrappedCompletion)
}
func After(after: NSTimeInterval, op: () -> (), completion: (() -> Void)?) {
let seconds = Int64(after * Double(NSEC_PER_SEC))
let 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