Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRickey Paape
24,122 PointsTIC Read Status Error After Refactor with Codable
I receive the following error: 2019-08-02 11:48:19.149541-0500 Stormy[5253:1207279] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7fd801500b20] get output frames failed, state 8196 2019-08-02 11:48:19.149680-0500 Stormy[5253:1207279] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x7fd801500b20] get output frames failed, state 8196 2019-08-02 11:48:19.150243-0500 Stormy[5253:1207279] TIC Read Status [1:0x0]: 1:57 2019-08-02 11:48:19.150323-0500 Stormy[5253:1207279] TIC Read Status [1:0x0]: 1:57
Here is my DarkSkyAPIClient: '''Swift // // DarkSpyAPIClient.swift // Stormy // import Foundation
class DarkSkyAPIClient { fileprivate let darkSkyApiKey = "key"
lazy var baseUrl : URL = {
return URL(string: "https://api.darksky.net/forecast/\(darkSkyApiKey)/")!
}()
let decoder = JSONDecoder()
let session: URLSession
init(configuration: URLSessionConfiguration) {
self.session = URLSession(configuration: configuration)
}
convenience init() {
self.init(configuration: .default)
}
typealias WeatherCompletionHandler = (Weather?, Error?) -> Void
typealias CurrentWeatherCompletionHandler = (CurrentWeather?, Error?) -> Void
private func getWeather(at coordinate: Coordinate, completionHandler completion: @escaping WeatherCompletionHandler) {
guard let url = URL(string: coordinate.description, relativeTo: baseUrl) else {
completion(nil, DarkSkyError.invalidURL)
return
}
let request = URLRequest(url: url)
let task = session.dataTask(with: request) {data, response, error in
DispatchQueue.main.async {
if let data = data {
guard let httpResponse = response as? HTTPURLResponse else {
completion(nil, DarkSkyError.requestFailed)
return
}
if httpResponse.statusCode == 200 {
do {
let weather = try self.decoder.decode(Weather.self, from: data)
completion(weather, nil)
} catch let error {
completion(nil, error)
}
} else {
completion(nil, DarkSkyError.invalidData)
}
} else if let error = error {
completion(nil, error)
}
}
}
task.resume()
}
func getCurrentWeather(at coordinate: Coordinate, completionHandler completion: @escaping CurrentWeatherCompletionHandler) {
getWeather(at: coordinate) { weather, error in
completion(weather?.currently, error)
}
}
}
1 Answer
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsYou're getting this error as you're trying to edit the UI via a background thread. So you need to transfer onto the main thread after the networking in session is complete. So like this:
DispatchQueue.main.async {
if let data = data {
guard let httpResponse = response as? HTTPURLResponse else {
completion(nil, DarkSkyError.requestFailed)
return
}
if httpResponse.statusCode == 200 {
do {
let weather = try self.decoder.decode(Weather.self, from: data)
completion(weather, nil)
} catch let error {
completion(nil, error)
}
} else {
completion(nil, DarkSkyError.invalidData)
}
} else if let error = error {
completion(nil, error)
}
}
Rickey Paape
24,122 PointsRickey Paape
24,122 PointsI have updated the code to include the DispatchQueue.main.async, however, I am still receiving the same error and the weather information is not updating.
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsDaniel Turato
Java Web Development Techdegree Graduate 30,124 PointsDid you purposely delete your api key for viewing purposes here? If you didn't you would need an api key from the DarkSky website