I have a method which should support being called from any queue, and should expect to.
It runs some code in a background thread itself, and then uses dispatch
There is actually still a way to compare the queue.
When you set up your queue, make sure that you add the label. For my purposes, I have a shared queue that is used for accessing a database to prevent database locking. In my DB.m file I have defined the shared queue function like:
const char *kTransactionQueueLabel = "DB_TRANSACTION_DISPATCH_QUEUE";
+ (dispatch_queue_t)sharedDBTransactionQueue {
static dispatch_queue_t sharedDBQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDBQueue = dispatch_queue_create(kTransactionQueueLabel, DISPATCH_QUEUE_SERIAL);
});
return sharedDBQueue;
}
The shared db transaction queue is used locally in the file to dispatch all executions to the database. However, there is also a public accessor on this to allow dispatching entire transactions to the database. So internally, if a DB access method is called from within the transaction queue, we need to dispatch internally on a different queue (all synchronous dispatches). So internally, I always dispatch on the proper queue by using the below getter.
/**
* @description Decide which queue to use - if we are already in a transaction, use the internal access queue, otherwise use the shared transaction queue.
*/
- (dispatch_queue_t)getProperQueueForExecution {
const char *currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
dispatch_queue_t sharedAccessQueue = [DB sharedDBTransactionQueue];
if (strcmp(currentLabel, kTransactionQueueLabel) == 0) {
sharedAccessQueue = [DB sharedInternalDBAccessQueue];
}
return sharedAccessQueue;
}
Hopefully this helps. Sorry for the long example. The Gist of it is that you can use
const char *currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
to get the label of the current queue and compare to a defined label.