Last active
October 28, 2015 14:24
-
-
Save loganwright/4ca3b952a622f2bcb990 to your computer and use it in GitHub Desktop.
After
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
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