Get current dispatch queue?
Solution 1:
If you are working with an NSOperationQueue
, it can provide the current dispatch queue for you.
NSOperationQueue has the class function [NSOperationQueue currentQueue]
, which returns the current queue as a NSOperationQueue object. To get the dispatch queue object you can use [NSOperationQueue currentQueue].underlyingQueue
, which returns your currrent queue as a dispatch_queue_t
.
Swift 3:
if let currentDispatch = OperationQueue.current?.underlyingQueue {
print(currentDispatch)
}
- works for main queue!
Solution 2:
With the deprecation of dispatch_get_current_queue()
there is effectively no way to know what queue you're executing on. If you peruse the GCD sources, you'll eventually see that this is because there may be multiple answers to the question "what queue am I executing on?" (Because queues eventually target one of the global queues, etc.)
If you want to guarantee that a future block is run on a specific queue, then the only way is to make your API accept a queue as a parameter along with the completion block. This lets the caller decide where the completion gets executed.
If simply knowing whether the caller is on the main thread or not is enough, you can use +[NSThread isMainThread]
to find out. In the common case, all blocks executing on the main GCD queue will be executing on the main thread. (One exception to this rule is if your application uses dispatch_main()
in lieu of a main run loop, you will have to use dispatch_get_specific
and friends to detect with certainty that you are executing on the main queue -- this is a comparatively rare circumstance.) More commonly, note that not all code that executes on the main thread executes on the main queue via GCD; GCD is subordinate to the main thread runloop. For your specific case it sounds like that might be enough.
Solution 3:
You do have the option of "dispatch_get_current_queue()
", however the iOS 6.1 SDK defines this API with these disclaimers:
"Recommended for debugging and logging purposes only:
"
and
"This function is deprecated and will be removed in a future release.
".
Here's another related question with some alternatives you can consider if you want code that's future-proof.
Solution 4:
With the deprecation of dispatch_get_current_queue()
you cannot directly get a pointer to the queue you are running on, however you do can get the current queue's label by calling dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)
and that does give you some flexibility.
You can always check if you are on that specific queue just by comparing their labels, so in your case if you don't want to force it on main queue, when you entered the method you can just utilize the following flag:
let isOnMainQueue = (dispatch_queue_get_label(dispatch_get_main_queue()) == dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL))
If you are running on the global queue, you will respectfully get the queue's label associated with it's QOS type, which can be one of the following:
com.apple.root.user-interactive-qos //qos_class_t(rawValue: 33)
com.apple.root.user-initiated-qos //qos_class_t(rawValue: 25)
com.apple.root.default-qos //qos_class_t(rawValue: 21)
com.apple.root.utility-qos //qos_class_t(rawValue: 17)
com.apple.root.background-qos //qos_class_t(rawValue: 9)
And then you can use dispatch_get_global_queue(qos_class_self(), 0)
which will give you back that same global queue you are running on.
But I believe Apple particularly discourages us from bounding the logic to the queue we got called on, so better utilising this for exclusively debugging purposes.