eduardo eduardo - 10 months ago 94
HTTP Question

How to make simultaneous async HTTP requests in swift 3, without AlamoFire

I wanna make a web crawling, currently i am reading a txt file with 12000 urls, i wanna use concurrency in this process, but the requests don't work.

typealias escHandler = ( URLResponse?, Data? ) -> Void

func getRequest(url : URL, _ handler : @escaping escHandler){

let session = URLSession(
configuration: .default,
delegate: nil,
delegateQueue: nil)

var request = URLRequest(url:url)

request.httpMethod = "GET"

let task = session.dataTask(with: request){ (data,response,error) in




for sUrl in textFile.components(separatedBy: "\n"){
let url = URL(string: sUrl)!

getRequest(url: url){ response,data in



Answer Source

If you have your URLSessions working correctly, all you need to go is create separate OperationQueue create a Operation for each of your async tasks you want completed, add it to your operation queue, and set your OperationQueue's maxConcurrentOperationCount to control how many of your tasks can run at one time. Puesdo code:

let operationQueue = OperationQueue()
operationQueue.qualityOfService = .utility

let exOperation = BlockOperation(block: {
    //Your URLSessions go here.
exOperation.completionBlock = {
    // A completionBlock if needed


Using a OperationQueue subclass and Operation subclass will give you additional utilities for dealing with multiple threads.