Skip to content

Instantly share code, notes, and snippets.

@gtokman
Last active January 28, 2021 22:04
Show Gist options
  • Save gtokman/4086635af2029f056ef181c82cdfd050 to your computer and use it in GitHub Desktop.
Save gtokman/4086635af2029f056ef181c82cdfd050 to your computer and use it in GitHub Desktop.
WeatherApp Debug
func fetchForecast(for location: CLLocation) -> AnyPublisher<API, Error> {
// before
let started = Date()
var components = URLComponents()
components.path = "/data/2.5/onecall"
components.queryItems = [
"lat": String(location.coordinate.latitude),
"lon": String(location.coordinate.longitude),
"units": "imperial",
"appid": "",
].compactMap {
URLQueryItem(name: $0.key, value: $0.value)
}
let url = components.url(relativeTo: URL(string: "https://api.openweathermap.org")!)
let request = URLRequest(url: url!)
return URLSession
.shared
.dataTaskPublisher(for: request)
.tryMap {
// after
let interval = Date().timeIntervalSince(started)
print("****************** \(interval)") // ****************** 0.32913994789123535
return try JSONDecoder().decode(API.self, from: $0.data)
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment