iOS Swift: Initiate the download task for 100 URLs at a time (concurrent) in less than 20 seconds

155 Views Asked by At

In one of my project, we are working on one interesting feature, that is we have a rest json response, it contains 100 media download urls. Because of some security reasons, from response received time those URL's are valid for 20 seconds to start the download process. Here important thing is all 100 URL's should start the download process with in 20 seconds and those can be complete in later we don't have any issue, but important thing is those should start the download process with in 20 seconds from REST response time.

We have concurrent multi threading concept in iOS, but i am not sure, all the threads will initiate the download process for 100 urls in less than 20 seconds.

Please let me know your thoughts on this. Thanks in advance.

1

There are 1 best solutions below

0
On

I would use the following approach:

let urls = [URL]() // your URLs here

let queue = OperationQueue()
queue.maxConcurrentOperationCount = 120 // to have capacity for all operations
queue.qualityOfService = .userInitiated // to have higher priority of start

let operations = urls.map { url in
    BlockOperation(block: {
        // load url code here, better as one function call here
    })
}
queue.addOperations(operations, waitUntilFinished: false)