user403015
user403015

Reputation: 7227

How to get the tasks list in GCD queue?

I get the main queue in GCD as below and I add different tasks from different classes in my apps.

dispatch_queue_t queue = dispatch_get_global_queue (
    DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

Now I want to know how many of my tasks which are still in the GCD main queue.

Is there any method to get the tasks list in GCD queue?

Thanks

Upvotes: 4

Views: 2505

Answers (2)

Rob Napier
Rob Napier

Reputation: 299485

Typically this is done with dispatch groups rather than queues. You can assign tasks to a group using dispatch_group_async(), or you can manually mark things in the group using dispatch_group_enter() and dispatch_group_leave(). You can then check for whether there is anything in the group using either dispatch_group_notify() or dispatch_group_wait().

Upvotes: 2

borrrden
borrrden

Reputation: 33421

That's really not the paradigm of GCD. If you want to keep track of a certain group of operations, for example, you can create a dispatch group and sign up to be notified when it is done, as in this example.

dispatch_group_t taskGroup = dispatch_group_create();
dispatch_queue_t queue = //Get whatever queue you want here
dispatch_group_async(taskGroup, queue, ^ {
    [object doSomething];
});
dispatch_group_async(taskGroup, queue, ^ {
    [object doMoreStuff];
});
dispatch_group_async(taskGroup, queue, ^ {
    [object doEvenMoreStuff];
});
dispatch_group_notify(taskGroup, queue, ^{
    [object workDone];
});
dispatch_release(taskGroup);

Upvotes: 4

Related Questions