Questions tagged [dispatch-after]

Enqueue a block for execution at the specified time.

This function waits until the specified time and then asynchronously adds block to the specified queue.

Passing DISPATCH_TIME_NOW as the when parameter is supported, but is not as optimal as calling dispatch_async instead. Passing DISPATCH_TIME_FOREVER is undefined.

7 questions
420
votes
15 answers

How to create dispatch queue in Swift 3

In Swift 2, I was able to create queue with the following code: let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT) But this doesn't compile in Swift 3. What is the preferred way to write this in Swift…
user4790024
406
votes
8 answers

How to program a delay in Swift 3

In earlier versions of Swift, one could create a delay with the following code: let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { //put your code which should…
owlswipe
  • 19,159
  • 9
  • 37
  • 82
3
votes
0 answers

Is dispatch_after more efficient than NSTimer when used in large quantities?

Which is more efficient? The biggest difference is you can't cancel dispatch_after blocks from executing while you can with NSTimer. So which is better: a large number of dispatch_after blocks that can't be canceled or a large number of NSTimers…
Tony Friz
  • 883
  • 1
  • 10
  • 27
2
votes
2 answers

dispatch_after block is not running

Please consider this simple example: - (void)viewDidLoad { [super viewDidLoad]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW,…
2
votes
1 answer

How does DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) work in Swift 3?

In Swift 3 the syntax of GCD has changed quite a bit. A call to dispatch_after() now looks like this: DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {//do something} That code would invoke the block 5 seconds after it's called. How does that…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
1
vote
1 answer

dispatch_get_main_queue() doesn't run new async jobs smoothly

let dispatchGroup = dispatch_group_create() let now = DISPATCH_TIME_NOW for i in 0..<1000 { dispatch_group_enter(dispatchGroup) // Do some async tasks let delay = dispatch_time(now, Int64(Double(i) * 0.1 * Double(NSEC_PER_SEC))) …
zs2020
  • 53,766
  • 29
  • 154
  • 219
0
votes
1 answer

dispatch_after not always working

I tried our the following simple test to understand the QoS questions in Interaction between qualityOfService property of NSOperationQueue & NSOperation added to it While doing this, I am running into a weird issue, where the code inside a…