I have an array of video URLs that need to be downloaded. To keep the overall performance, I want to limit the concurrent download counts to 2. For example, I want to start the first two download tasks concurrently at the beginning, then start the third when any of the previous download finishes. At any given time, no more than 2 downloads are executing at the same time. How to achieve this?
It first occurred to me that I should set NSOperationQueue.maxConcurrentOperationCount to 2, then wrapper a NSURLSessionDataTask into a NSOperation and put it in the NSOpeartionQueue. Later I realized that [NSURLSessionDataTask resume] is an asynchronous operation, so all the downloads will start executing almost at the same time.
Another thought is that I put all the tasks in a pending queue first and start the first two downloads. When any task is finished, I check if there are more tasks waiting in the queue and start one if there is.
Is there any better solution?