Swift3 URL taking a long time to return with data

Please help me.

I want to get an object of data from http.

It works without problems with the code shown below.

However, if you can not access http, it takes time to process "Data (contentsOf: url)".

It will be around 20~30 seconds or so.

Please tell me about how I can deal with something.


---func---

let url = URL(string:"http://~~~~")!

var image: UIImage = UIImage()

autoreleasepool {

guard let sampleData = try? Data(contentsOf: url) else {

return

}

Image = UIImage(data: sampleData)!

}

DispatchQueue.main.async {

self.sampleView.image = Image

}

-----

Replies

The most likely cause of this is networking delays. As a first step, I recommend you fetch the data with an actual networking API, namely, URLSession, at which point you’ll be can get a high-level overview of the networking performance by way of the

URLSessionTaskMetrics
value.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"