Now that dispatch_get_current_queue
is deprecated in iOS 6, how do I use dispatch_after
to execute something in the current queue?
The various links in the comments don't say "it's better not to do it." They say you can't do it. You must either pass the queue you want or dispatch to a known queue. Dispatch queues don't have the concept of "current." Blocks often feed from one queue to another (called "targeting"). By the time you're actually running, the "current" queue is not really meaningful, and relying on it can (and historically did) lead to dead-lock. dispatch_get_current_queue()
was never meant for dispatching; it was a debugging method. That's why it was removed (since people treated it as if it meant something meaningful).
If you need that kind of higher-level book-keeping, use an NSOperationQueue
which tracks its original queue (and has a simpler queuing model that makes "original queue" much more meaningful).
There are several approaches used in UIKit that are appropriate:
[NSURLConnection setDelegateQueue:]
or addObserverForName:object:queue:usingBlock:
for examples. Notice that NSURLConnection
expects an NSOperationQueue
, not a dispatch_queue
. Higher-level APIs and all that.NSURLConnection
historically worked before queues.Create a queue manually and dispatch both your calling code and your dispatch_after
code onto that. That way you can guarantee that both pieces of code are run from the same queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With