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.
Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue.
The main dispatch queue is a globally available serial queue executing tasks on the application's main thread.
CONCURRENT QUEUES (often known as global dispatch queues) can execute tasks simultaneously; the tasks are, however, guaranteed to initiate in the order that they were added to that specific queue, but unlike serial queues, the queue does not wait for the first task to finish before starting the second task.
serial when creating a serial queue: let serialQueue = DispatchQueue(label: "queuename") . In Xcode 8 beta 4 there is no . serial option so you have to create serial queue by omitting the . concurrent in attributes.
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."
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