Home Swift 4: How to asynchronously use URLSessionDataTask but have the requests be in a timed queue?
 Basically I have some JSON data that I wish to retrieve from a bunch of URL's (all from the same host), however I can only request this data roughly every 2 seconds at minimum and only one at a time or I'll be "time banned" from the server. As you'll see below; while URLSession is very quick it also gets me time banned almost instantly when I have around 700 urls to get through. How would I go about creating a queue in URLSession (if its functionality supports it) and while having it work asynchronously to my main thread; have it work serially on its own thread and only attempt each item in the queue after 2 seconds have past since it finished the previous request? for url in urls { get(url: url) } func get(url: URL) { let session = URLSession.shared let task = session.dataTask(with: url, completionHandler: { (data, response, error) in if let error = error { DispatchQueue.main.async { print(error.localizedDescription) } return } let data = data! guard let response = response as? HTTPURLResponse, response.statusCode == 200 else { DispatchQueue.main.async { print("Server Error") } return } if response.mimeType == "application/json" { do { let json = try JSONSerialization.jsonObject(with: data) as! [String: Any] if json["success"] as! Bool == true { if let count = json["total_count"] as? Int { DispatchQueue.main.async { self.itemsCount.append(count) } } } } catch { print(error.localizedDescription) } } }) task.resume() }