Batch Request

You can use an AWFBatchLoader instance to request data from multiple endpoints in a single request by instantiating the respective object loaders for your data sets and adding them to your AWFBatchLoader instance. The following loadWeatherData could be used within your custom view controller or your project's network/service layer:

MyViewController.h

- (void)loadWeatherData {
    // reuse our AWFBatchLoader instance each time we request data since the data requests will remain the same
    if (!self.loader) {
        AWFBatchLoader *loader = [[AWFBatchLoader alloc] init];
        self.loader = loader;
        
        // add the necessary object loaders for the required data sets and assign them to the AWFBatchLoader using a reference key
        AWFObservationsLoader *obsLoader = [[AWFObservationsLoader alloc] init];
        [loader addLoader:obsLoader forKey:@"obs"];
        
        AWFSunMoonLoader *sunmoonLoader = [[AWFSunMoonLoader alloc] init];
        sunmoonLoader.options.limit = 7;
        [loader addLoader:sunmoonLoader forKey:@"sunmoon"];
        
        AWFForecastsLoader *forecastsLoader = [[AWFForecastsLoader alloc] init];
        forecastsLoader.options.limit = 7;
        [loader addLoader:forecastsLoader forKey:@"forecast"];
    }

    // set the place to use for all object loaders associated with the AWFBatchLoader
    AWFPlace *place = [AWFPlace placeWithCity:@"seattle" state:@"wa" country:@"us"];
    [self.loader setPlaceForAllLoaders:place];

    __weak typeof(self) weakSelf = self;
    [self.loader getWithCompletionBlock:^(AWFBatchLoader *loader, NSError *error) {
        // grab the returned objects from the AWFBatchLoader request based on the object loaders associated by their keys set above
        NSArray *sunmoon = [loader objectsForLoaderWithKey:@"sunmoon"];
        NSArray *obs = [loader objectsForLoaderWithKey:@"obs"];
        NSArray *forecasts = [loader objectsForLoaderWithKey:@"forecast"];
        
        if ([obs count] > 0) {
            AWFObservation *ob = [obs firstObject];
            // update obs data...
        }
        if ([forecasts count] > 0) {
            AWFForecast *forecast = [forecasts firstObject];
            NSArray *periods = forecast.periods;
            // update forecast data...
        }
        if (sunmoon count] > 0) {
            // update sun/moon data...
        }
    }];
}

MyViewController.swift

func loadWeatherData() {
	if loader == nil {
		loader = AWFBatchLoader()
		
		let obsLoader = AWFObservationsLoader()
		loader?.addLoader(obsLoader, forKey: "obs")
		
		let sunmoonLoader = AWFSunMoonLoader()
		sunmoonLoader.options.limit = 7
		loader?.addLoader(sunmoonLoader, forKey: "sunmoon")
		
		let forecastsLoader = AWFForecastsLoader()
		forecastsLoader.options.limit = 7
		loader?.addLoader(forecastsLoader, forKey: "forecast")
	}
	
	let place = AWFPlace(city: "seattle", state: "wa", country: "us")
	loader?.setPlaceForAllLoaders(place)
	
	loader?.getWithCompletionBlock({ (loader, error) in
		// grab the returned objects from the AWFBatchLoader request based on the object loaders associated by their keys
		if let results = loader?.objectsForLoader(withKey: "obs"), let ob = results.first as? AWFObservation {
			// update obs data
		}
		
		if let results = loader?.objectsForLoader(withKey: "forecast"), let forecast = results.first as? AWFForecast {
			let periods = forecast.periods
			// update forecast data
		}
		
		if let results = loader?.objectsForLoader(withKey: "sunmoon"), results.count > 0 {
			// update sun/moon data...
		}
	})
}

Last modified: November 02, 2017