Is there a way to log the thread or queue that a method is running on / called from? Something like:
- (void)foo
{
NSLog(@"Running on %@ queue, %@ thread", queue, thread);
}
You can get the current thread with +[NSThread currentThread]
. That could have a name
property, but if you didn't set one don't rely on it.
Queues are trickier because there are different meanings of "queue". A queue could be an NSOperationQueue
, and you can grab its name
from +[NSOperationQueue currentQueue]
(again, assuming you set it).
Then there are dispatch queues. You can get the current queue with dispatch_get_current_queue()
, but be warned that this function will succeed even if called from code that isn't associated with a queue(!). In that case it returns the default background queue Queues are labeled, so you can call dispatch_queue_get_label()
and if you created the queue with a label, you will get that.
So basically, yes you can get the queue or thread—with the proviso that all code has an associated dispatch queue even if it isn't code that was dispatched. You can also usually get meaningful names for these threads and queues, which is handy for debugging: but it's your responsibility to name them.