I'm quite new to Swift and have a problem that I can't solve since about 1,5 weeks. I was checking every forum if anyone has the same problem, but I couldn't find anyone. Would be cool if someone could help me. I want to get data from an website through parsing json.
My code to get the data looks like this:
func getData(){
let url = URL(string: "https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=EUR")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print("ERROR")
} else
{
if let content = data
{
do
{
/
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
let EthereumDataEUR = try JSONDecoder().decode([ethereumDataEUR].self, from: data!)
self.course = Double(EthereumDataEUR[0].price_eur)!
}
catch
{
}
}
}
}; task.resume()
label.text = String(format: "%.2f", course)
}
Every time I start the code in the simulator or real Device (it doesn't matter which one) I get the following code in the console:
2017-11-20 14:40:23.813604+0100 JsonTest[17534:5500248] [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1757 received sct extension length is less than sct data length
I am using this code in viewWillAppear, so that the user has latest information when he starts the app or changes between tab bars. And I also use it in an extra function when he clicks on a button. I only get the boringSSl error when the data is downloaded the first time. I have got labels where the data that I download should be displayed, but it doesn't display after the first download. I hope you know what i mean? 🙂
Thanks for your help.
On the second click on the button, I don't get the boringSSL error …
Right. That’s very likely the result of some sort of caching in the networking layer, for example:
URLCacheHTTP 1.1 or HTTP 2 connection cache
This caching means that the networking stack does less work, which means it never runs the code that generates this log message.
If my theory is correct then this log message is completely unrelated to your ‘data not displaying’ problem. You can test that by adding some log statements to the first line of the closure you pass to
dataTask(with:).
print(data)
print(response)
print(error)I suspect you’ll see that this closure is called with the correct data in both cases.
As to why the data isn’t showing up in your UI, I suspect that’s because you’re setting it from a secondary thread. Remember that:
calls its completion closure on some unspecified secondary threaddataTask(with:)UIKit can only be called from the main thread
If you call UIKit from a secondary thread you will encounter all sorts of weird problems, including delays like the ones you’re currently seeing.
A good way to flush out issues like this is to enable Main Thread Checker in the Diagnostics tab of your Run scheme.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"