I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it:
Can someone explain the structure of dispatch_after
?
d
This worked for me.
Swift 3:
let time1 = 8.23
let time2 = 3.42
// Delay 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
print("Sum of times: \(time1 + time2)")
}
Objective-C:
CGFloat time1 = 3.49;
CGFloat time2 = 8.13;
// Delay 2 seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CGFloat newTime = time1 + time2;
NSLog(@"New time: %f", newTime);
});
For multiple functions use this. This is very helpful to use animations or Activity loader for static functions or any UI Update.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
// Call your function 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// Call your function 2
}
}
For example - Use a animation before a tableView reloads. Or any other UI update after the animation.
*// Start your amination*
self.startAnimation()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
*// The animation will execute depending on the delay time*
self.stopAnimation()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
*// Now update your view*
self.fetchData()
self.updateUI()
}
}
I always prefer to use extension instead of free functions.
Swift 4
public extension DispatchQueue {
private class func delay(delay: TimeInterval, closure: @escaping () -> Void) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
class func performAction(after seconds: TimeInterval, callBack: @escaping (() -> Void) ) {
DispatchQueue.delay(delay: seconds) {
callBack()
}
}
}
Use as follow.
DispatchQueue.performAction(after: 0.3) {
// Code Here
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// ...
});
The dispatch_after(_:_:_:)
function takes three parameters:
a delay
a dispatch queue
a block or closure
The dispatch_after(_:_:_:)
function invokes the block or closure on the dispatch queue that is passed to the function after a given delay. Note that the delay is created using the dispatch_time(_:_:)
function. Remember this because we also use this function in Swift.
I recommend to go through the tutorial Raywenderlich Dispatch tutorial
To execute a funtion or code after a delay use the next method
DispatchQueue.main.asyncAfter(deadline: .now() + 'secondsOfDelay') {
your code here...
}
Example - In this example the funtion getShowMovies
will be executed after 1 second
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.getShowMovies()
}
I use dispatch_after
so often that I wrote a top-level utility function to make the syntax simpler:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
And now you can talk like this:
delay(0.4) {
// do stuff
}
Wow, a language where you can improve the language. What could be better?
Seems almost not worth bothering with, now that they've improved the calling syntax:
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}