Loading Data

Loading Data

All requests for weather data to the Xweather Weather API from the Xweather iOS SDK are made using a series of endpoint service classes, which are instances of AWFWeatherEndpoint. Each API endpoint has its own service class within the SDK which handle setting up the request and parsing the API response into consistent, easy-to-use weather model objects. This means you don't have to worry about API urls or parsing any JSON data while ensuring the data is provided in strict data types from the models.

Each weather model object returned to your application from the endpoint services is a subclass of AWFWeatherObject whose data is transformed into the appropriate model class based on the endpoint service that was used.

To use a service class, you can either create your own instance or use the global shared instance:

// shared instance
let places = AWFPlaces.sharedService()
    
// separate instance
let places = AWFPlaces()

Providing Request Options

Before making your request using this service class instance, you'll need to setup any request options you may want, such as setting a limit or adding request filters. You can either set request options directly on an instance of a service class using the options property:

places.options.limit = 5
places.options.filterString = "poi"

Or, you can setup a new instance of AWFWeatherRequestOptions and pass it to the actual request:

let options = AWFWeatherRequestOptions()
options.limit = 5
options.filterString = "poi"

Note that when you set request options directly on a service class instance, such as AWFPlaces above, those options will be used with each request thereafter unless they are modified again. Therefore, it's recommended to create a seperate AWFWeatherRequestOptions instance and pass it to the actual request.

Performing the Request

Now you're ready to perform the request. The completion block for each request using a service class will return an instance of AWFWeatherEndpointResult containing the array of results and/or the error that may have occurred during the request:

let place = AWFPlace(city: "austin", state: "tx", country: "us")
AWFPlaces.sharedService().get(forPlace: place, options: options) { (result) in
    guard let results = result?.results else { print("Data failed to load - \(result?.error)"); return }
    
    if let place = results.first as? AWFPlace {
        // do something with the model...
    }
}

That's it! All endpoint service classes support the same methods, so it's easy to apply the same example above to other endpoints. Make sure to review the list of supported service classes and the model objects they return with requests.