Reputation: 992
I'm trying to download over 1000 images with total size of 50MB. My code for iOS 9 was:
let operationQueue = OperationQueue.main
operationQueue.maxConcurrentOperationCount = 1
operationQueue.qualityOfService = .background
for url in urls{
let urlRequest = URLRequest(url: URL(string: url)!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: operationQueue, completionHandler: { (response, data, error) in
//image
})
}
so I want to download the images one by one, but now in iOS 10 sendAsynchronousRequest is deprecated and I don't know how to add the images to queue. I saw different questions about using of sendAsynchronousRequest in iOS 10 , but I didn't find how to add it to queue. The most of the answers are to use URLSession.shared.dataTask(...)
, but there you can't add the task to a queue. Any suggestions how to add all requests to operationQueue ?
Upvotes: 0
Views: 64
Reputation: 285160
URLSession
is the right way to go.
Unlike in NSURLConnection
where the request is dispatched on a queue, URLSession
itself is dispatched on a queue.
Create a custom session with
init(configuration: URLSessionConfiguration,
delegate: URLSessionDelegate?,
delegateQueue queue: OperationQueue?)
It's worth it to read the documentation
Upvotes: 1