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.
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
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
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
URLError.badURL- If the URL cannot be constructedURLError.badServerResponse- If the server returns a non-200 status codeNSError(domain: “AirQualityService”, code: 1) - If no air quality data is available in the responseDecodingError- If the response cannot be decoded
latitudeandlongitudefor the locationcurrentparameter requesting multiple pollutant measurementstimezoneset 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[(city: TopCity, aqi: Int?)] - An array of tuples containing:
city: The originalTopCityobjectaqi: The US AQI value for that city, ornilif data couldn’t be fetched
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.