Skip to main content

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.
Source: 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

A singleton instance of the service that can be accessed throughout the application.

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
Returns: [ClimateDataPoint] - An array of climate data points, sorted by year (ascending), each containing:
  • year: The year of the data point
  • temperature: The maximum temperature (°C) for that date in that year
Throws: This method does not throw errors. Individual year fetch failures are caught and logged, but don’t prevent the method from returning partial results. Implementation Details: The method fetches temperature data for the current date across the following years:
  • 1980
  • 1990
  • 2000
  • 2010
  • 2020
  • Current year
For each year, it:
  1. Constructs the date string in YYYY-MM-DD format
  2. Makes an API request to the Open-Meteo Archive API
  3. Extracts the maximum temperature for that date
  4. Handles errors gracefully, logging failures without throwing
Query Parameters:
  • latitude and longitude: Location coordinates
  • start_date and end_date: Same date for single-day data
  • daily: Set to temperature_2m_max for maximum temperature
  • timezone: 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:
  1. Catching individual year fetch failures
  2. Logging errors to the console
  3. Continuing to fetch data for remaining years
  4. Returning partial results if some years fail
This approach ensures that temporary network issues or missing data for specific years don’t prevent the entire operation from succeeding.

API Endpoint

The service uses the Open-Meteo Archive API:
This API provides historical weather data dating back to 1940.

Source Code

Performance Considerations

Sequential API Calls

The current implementation makes sequential API calls for each year. For better performance, consider fetching all years in parallel:

Caching

Historical climate data doesn’t change, making it ideal for caching:

See Also