Last active
January 28, 2021 22:04
-
-
Save gtokman/4086635af2029f056ef181c82cdfd050 to your computer and use it in GitHub Desktop.
WeatherApp Debug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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