Mazyod
Mazyod

Reputation: 22559

Can GCD on iOS handle hundreds of dispatched blocks?

I would like to utilize GCD for, say, a hundred objects that all need to download some data from the server. If I were to loop over these objects, and call something like:

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(q, ^{
    // Download data;
});

Would those blocks get intelligently queued and efficiently executed, or am I going to run into memory issues, performance issues, or even race conditions?

My two cents are that the blocks will be queued as the name suggests and download will start one at a time, and as long as I am cleaning up properly if the application terminates before all the downloads are complete, there shouldn't be problems.

However, another bonus question:

Would I benefit more if I were to create, say, 3-5 queues and download multiple files at any one time by distributing the downloads amongst the queues?


After implementing this functionality, it seems that the accepted answer is not complete, or I missed the point. Dispatching blocks on a global queue, which is a concurrent queue could lead to issues, since there is a maximum number of threads that can be dispatched by GCD (~64). This is only true for concurrent queues, since they need to spawn a thread for each operation. If you, however, create your own queue, that queue will be a sequential queue that executes the blocks one after the other, even when dispatch_async is called. This way, you can be certain that your queue will only spawn 1 thread, and queue your operations, and never reach a thread limit issue.

Upvotes: 2

Views: 259

Answers (2)

James Bush
James Bush

Reputation: 1527

From Concurrent Programming in MacOS X and iOS (O'Reilly):

enter image description here

Upvotes: 0

Kumar Aditya
Kumar Aditya

Reputation: 1097

Addressing your first bunch of question i.e. "Would those blocks get intelligently queued and efficiently executed, or am I going to run into memory issues, performance issues, or even race conditions".

My answers:

  1. Would those blocks get intelligently queued and efficiently executed : Yes, they will without any doubt.

  2. am I going to run into memory issues, performance issues : This is situation dependent. As you are calling async method, so it wont guarantee that second download operation will start after the completion of first one. If you are downloding images or video you may have issues of running out of memory issues because of CFDATA or CFDATA(store) issues, (which can be handled).

  3. or even race conditions : You will never get caught in race condition if you know well how and when to switch threads. For eg : IF you download class delegates need to be called on main thread you will need to start connection on main thread like in NSURLConnection. If you are dealing with UI elements after donwload you will still need to switch your thread. Other wise no race condition or deadlock will occur.

Addressing your second bunch of question "Would I benefit more if I were to create, say, 3-5 queues and download multiple files at any one time by distributing the downloads amongst the queues?"

If I would be in your place I would have gone with single queue for hundreds of objects. I have been in this situation and in my case I have to download thousands of file. I would go for single file download at a time and do cleaning up and then move forward. As if you have hundreds of file to be downloaded even 0.1 MB of extra allocation will cause you a performance issue.

Upvotes: 4

Related Questions