Overview
ClimateService is a Swift actor that fetches historical temperature data from the Open-Meteo Archive API. It retrieves temperature data for specific dates across multiple years, enabling climate trend analysis and historical comparisons.
BreezeApp/Services/ClimateService.swift:4
Actor Isolation
This service is implemented as an actor to ensure thread-safe access to its methods and properties. All async methods are actor-isolated and can be safely called from any context.Shared Instance
Methods
fetchClimateData
Fetch historical temperature data for a specific location across multiple years.Double
required
The latitude of the location to fetch climate data for
Double
required
The longitude of the location to fetch climate data for
[ClimateDataPoint] - An array of climate data points, sorted by year (ascending), each containing:
year: The year of the data pointtemperature: The maximum temperature (°C) for that date in that year
- 1980
- 1990
- 2000
- 2010
- 2020
- Current year
- Constructs the date string in
YYYY-MM-DDformat - Makes an API request to the Open-Meteo Archive API
- Extracts the maximum temperature for that date
- Handles errors gracefully, logging failures without throwing
latitudeandlongitude: Location coordinatesstart_dateandend_date: Same date for single-day datadaily: Set totemperature_2m_maxfor maximum temperaturetimezone: Set to “auto” for automatic timezone detection
Usage Examples
Basic Usage
Calculate Temperature Trend
Display in SwiftUI Chart
Error Handling
The service handles errors gracefully by:- Catching individual year fetch failures
- Logging errors to the console
- Continuing to fetch data for remaining years
- Returning partial results if some years fail