Batch Request

Batch request support is available with the Aeris Weather SDK for iOS, 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 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:

ObjectiveC
Swift
AWFObservations *obs = [AWFObservations new];

AWFForecasts *hourlyForecasts = [AWFForecasts new];
hourlyForecasts.options.filterString = @"1hr";
hourlyForecasts.options.limit = 12;

AWFForecasts *forecasts = [AWFForecasts new];
forecasts.options.filterString = @"day";
forecasts.options.limit = 7;

AWFAdvisories *advisories = [AWFAdvisories new];

AWFBatchRequest *request = [[AWFBatchRequest alloc] initWithEndpoints:@[obs, hourlyForecasts, forecasts, advisories]];

// use the same place for all endpoints
AWFPlace *place = [AWFPlace placeWithCity:@"houston" state:@"tx" country:@"us"];
[request setPlaceForAllEndpoints:place];

[request getWithOptions:nil completion:^(AWFBatchRequest *request, NSError *error) {
    if (error) {
        NSLog(@"Batch request failed! - %@", error.localizedDescription);
        return;
    }

    AWFObservation *obsResult = (AWFObservation *)[[request resultsForEndpoint:obs] firstObject];
    AWFForecast *hourlyResult = (AWFForecast *)[[request resultsForEndpoint:hourlyForecast] firstObject];
    AWFForecast *forecastResult = (AWFForecast *)[[request resultsForEndpoint:forecasts] firstObject];
    NSArray *advisoryResults = [request resultsForEndpoint:advisories];

    if (obsResult) {
        NSLog(@"The current weather in %@ is %@ with a temperature of %.0f.", obsResult.place.name, obsResult.weather, obsResult.tempF);
    }

    NSLog(@"total hourly forecast periods: %i", [hourlyResult.periods count]);
    NSLog(@"total daily forecast periods: %i", [forecastResult.periods count]);
    NSLog(@"total advisories: %i", [advisoryResults count]);
}];
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:

ObjectiveC
Swift
AWFBatchRequest *request = [AWFBatchRequest new];

AWFObservations *obs = [AWFObservations new];
[request addEndpoint:obs forKey:@"obs"];

AWFForecasts *hourlyForecasts = [AWFForecasts new];
hourlyForecasts.options.filterString = @"1hr";
hourlyForecasts.options.limit = 12;
[request addEndpoint:hourlyForecasts forKey:@"hourly"];

AWFForecasts *forecasts = [AWFForecasts new];
forecasts.options.filterString = @"day";
forecasts.options.limit = 7;
[request addEndpoint:forecasts forKey:@"forecast"];

AWFAdvisories *advisories = [AWFAdvisories new];
[request addEndpoint:advisories forKey:@"advisories"];

// use the same place for all endpoints
AWFPlace *place = [AWFPlace placeWithCity:@"houston" state:@"tx" country:@"us"];
[request setPlaceForAllEndpoints:place];

[request getWithOptions:nil completion:^(AWFBatchRequest *request, NSError *error) {
    if (error) {
        NSLog(@"Batch request failed! - %@", error.localizedDescription);
        return;
    }

    AWFObservation *obsResult = (AWFObservation *)[[request resultsForEndpointWithKey:@"obs"] firstObject];
    AWFForecast *hourlyResult = (AWFForecast *)[[request resultsForEndpointWithKey:@"hourly"] firstObject];
    AWFForecast *forecastResult = (AWFForecast *)[[request resultsForEndpointWithKey:@"forecast"] firstObject];
    NSArray *advisoryResults = [request resultsForEndpointWithKey:@"advisories"];

    if (obsResult) {
        NSLog(@"The current weather in %@ is %@ with a temperature of %.0f.", obsResult.place.name, obsResult.weather, obsResult.tempF);
    }

    NSLog(@"total hourly forecast periods: %i", [hourlyResult.periods count]);
    NSLog(@"total daily forecast periods: %i", [forecastResult.periods count]);
    NSLog(@"total advisories: %i", [advisoryResults count]);
}];
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)")
}

Last modified: October 12, 2018