5
votes

I'm taking over ownership of a client app written by one of the client's employees who was new to iOS development (and has since left the company)

I'm trying to get a handle on/improve it's use of concurrency. It was creating a bunch of different GCD timers with different delays, and timing was a mess.

I'm probably going to convert it to use a GCD serial queue, since I need the tasks to be run in sequential order (but not on the main thread.) I'd like to monitor how deep the pending tasks get on the queue. NSOperationQueues have a facility for looking at the number of pending tasks, but I don't see similar option for GCD serial queues. Is there such a facility?

I guess I could build an NSOperationQueue, and make each operation depend on the previous operation, thus creating a serial operation queue, but that's a lot of work simply to diagnose the number of tasks that are in my queue at any one time.

1

1 Answers

11
votes

There is no public API to query the number of tasks in a GCD queue. You could either build something yourself, or use NSOperationQueue. If you wanted to build something, you could wrap dispatch_a/sync to increment a counter, and then enqueue another block with each operation that decrements the counter, like this:

int64_t numTasks = 0

void counted_dispatch_async(dispatch_queue_t queue, dispatch_block_t block)
{
    OSAtomicIncrement64(&numTasks);
    dispatch_async(queue, ^{
        block();
        OSAtomicDecrement64(&numTasks);
    });
}

Or you could just use NSOperationQueue. That's what I'd do. A wise man once told me, "Use the highest level abstraction that gets the job done."