Skip to main content

Overview

AirQualityService is a Swift actor that provides methods to fetch current air quality data from the Open-Meteo Air Quality API. It supports fetching data for single locations and batch fetching for multiple cities.
Source: BreezeApp/Services/AirQualityService.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

fetchAirQuality

Fetch current air quality data for a specific location.
Double
required
The latitude of the location to fetch air quality data for
Double
required
The longitude of the location to fetch air quality data for
Returns: AirQuality - An object containing current air quality metrics including:
  • US AQI (Air Quality Index)
  • PM10 and PM2.5 particulate matter
  • Carbon monoxide levels
  • Nitrogen dioxide levels
  • Sulphur dioxide levels
  • Ozone levels
Throws:
  • URLError.badURL - If the URL cannot be constructed
  • URLError.badServerResponse - If the server returns a non-200 status code
  • NSError (domain: “AirQualityService”, code: 1) - If no air quality data is available in the response
  • DecodingError - If the response cannot be decoded
Implementation Details: The method constructs a URL with the following query parameters:
  • latitude and longitude for the location
  • current parameter requesting multiple pollutant measurements
  • timezone set to “auto” for automatic timezone detection

fetchMultipleCities

Fetch AQI data for multiple cities simultaneously, useful for ticker displays.
[TopCity]
required
An array of TopCity objects containing city information with latitude and longitude coordinates
Returns: [(city: TopCity, aqi: Int?)] - An array of tuples containing:
  • city: The original TopCity object
  • aqi: The US AQI value for that city, or nil if data couldn’t be fetched
Error Handling: This method does not throw errors. Instead, it returns nil AQI values for cities where data couldn’t be fetched. Errors are logged to the console. Implementation Details: The method uses batch fetching by sending comma-separated latitude and longitude values in a single API request. This is more efficient than making individual requests for each city.
The response is parsed to handle both array and single object responses from the API.

API Endpoint

The service uses the Open-Meteo Air Quality API:

Source Code

See Also