Examples
Batch requests

Batch requests

Batch request support is available with the Xweather iOS SDK, which is convenient for performing requests for data from multiple API endpoints with a single request. While each endpoint request still counts as a single access against your account's daily accesses, this method often results in much faster response times than performing each endpoint request individually.

Review our batch request (opens in a new tab) guide that explains its usage in more detail.

The following example uses a batch request to load the latest observation, 12-hour forecast, 7-day forecast and active advisories for Houston, TX:

let obs = AWFObservations()
 
let hourlyForecasts = AWFForecasts()
hourlyForecasts.options?.filterString = "1hr"
hourlyForecasts.options?.limit = 12
 
let forecasts = AWFForecasts()
forecasts.options?.filterString = "day"
forecasts.options?.limit = 7
 
let advisories = AWFAdvisories()
 
let request = AWFBatchRequest(endpoints: [obs, hourlyForecasts, forecasts, advisories])
 
// use the same place for all endpoints
let place = AWFPlace(city: "houston", state: "tx", country: "us")
request.setPlaceForAllEndpoints(place)
 
request.get(options: nil) { (request, error) in
    guard error == nil else {
        print("Batch request failed! \(error)")
        return
    }
 
    if let obs = request.results(for: obs)?.first as? AWFObservation {
        print("The current weather in \(place.name) is \(obs.weather) with a temperature of \(obs.tempF).")
    }
 
    print("total hourly forecast periods: \(request.results(for: hourlyForecasts)?.count)")
    print("total hourly forecast periods: \(request.results(for: forecasts)?.count)")
    print("total hourly forecast periods: \(request.results(for: advisories)?.count)")
}

Alternatively, you can also assign endpoints and access request results using unique keys per endpoint added to the request:

let request = AWFBatchRequest()
 
let obs = AWFObservations()
request.addEndpoint(obs, forKey: "obs")
 
let hourlyForecasts = AWFForecasts()
hourlyForecasts.options?.filterString = "1hr"
hourlyForecasts.options?.limit = 12
request.addEndpoint(hourlyForecasts, forKey: "hourly")
 
let forecasts = AWFForecasts()
forecasts.options?.filterString = "day"
forecasts.options?.limit = 7
request.addEndpoint(forecasts, forKey: "forecast")
 
let advisories = AWFAdvisories()
request.addEndpoint(advisories, forKey: "advisories")
 
// use the same place for all endpoints
let place = AWFPlace(city: "houston", state: "tx", country: "us")
request.setPlaceForAllEndpoints(place)
 
request.get(options: nil) { (request, error) in
    guard error == nil else {
        print("Batch request failed! \(error)")
        return
    }
 
    if let obs = request.resultsForEndpoint(withKey: "obs")?.first as? AWFObservation {
        print("The current weather in \(place.name) is \(obs.weather) with a temperature of \(obs.tempF).")
    }
 
    print("total hourly forecast periods: \(request.resultsForEndpoint(withKey: "hourly")?.count)")
    print("total hourly forecast periods: \(request.resultsForEndpoint(withKey: "forecast")?.count)")
    print("total hourly forecast periods: \(request.resultsForEndpoint(withKey: "advisories")?.count)")
}