John Murray
John Murray

Reputation: 1

How to determine the currently running async GCD Dispatch Queues

I have a refresh button on my iOS application that launches an asynchronous dispatch queue in GCD. The name of the queue is custom. There can be issues where the user bangs the heck out of the button and causes a large amount of unnecessary queues to be created. My hope is to check to see if there is a queue with a specific name active so I could not launch another queue or add to the same queue of the same name.

Is this possible?

Upvotes: 0

Views: 762

Answers (3)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

I don't think you should create new queues on every request. And since you don't seem to worried about them being sequential as you're creating new queues to execute each block, I suggest you use the global queues to run your blocks. Both actions are synonymous as these are final target queues for the dispatch queues you spawn. Getting the queue is simple and should replace the code where you create your own queue.

dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

In case you want them to be run sequentially, you should define your own queue as an instance variable so that you create it only once and use the same one every time you need to dispatch a block.

Upvotes: 2

Kazuki Sakamoto
Kazuki Sakamoto

Reputation: 14009

No, there are no method to get a serial queue from the system by name. You need to have the serial queue by yourself.

Upvotes: 0

Praveen S
Praveen S

Reputation: 10393

Well you can maintain a Mutable dictionary with the objects added

[dict setobject:<your object> forkey:<the queue name>]

When you are sending subsequent request then in the method you can check the following:

object = [dict objectforkey:<queue name>]

if (object == nil)
//send the request

When the queue operation is complete remove the key-object pair from the dictionary.

[dict removeobjectforkey: < queue name >

Upvotes: 0

Related Questions