Search code examples
iosjsonswiftswift2alamofire

iOS newbie: search id from a local json file then make request to server


I am learning iOS development. Currently, I started my hobby project which is a weather foracast app. I have found a open REST API, I can send request to get weather data for a city.

The REST API needs me to send request with city id instead of city name & it provides me a 20MB json file which contains city objects with fields city_id, city_name, longitude, latitude. I know I should create a City class to have those fields.

My app is supposed to allow user to input the city name, then, my app make request with the corresponding city id. My question is a about the best practice for this:

  1. Since I need all city ids, should I download the 20MB json file & embed it to my app? Does that mean my app size would be larger than 20MB ? Any better practice to handle this?

  2. If I have to put the 20MB json file in my project, what is the best way to parse the content to a list of City programmatically?

  3. If user input one city name, I feel if I scan all the cities in the json file, it is inefficient, what could be the efficient way to find the city id for the city name (without scanning all the 20MB json)?

==== THE OPEN REST API ===


Solution

  • You do not need to include the JSON in your app. In fact, I highly recommend against doing so. This is according to the documentation:

    Description:

    You can call by city name or city name and country code. API responds with a list of results that match a searching word. API call:

    api.openweathermap.org/data/2.5/weather?q={city name}

    api.openweathermap.org/data/2.5/weather?q={city name},{country code}

    Parameters:

    q city name and country code divided by comma, use ISO 3166 country codes

    Examples of API calls:

    api.openweathermap.org/data/2.5/weather?q=London

    api.openweathermap.org/data/2.5/weather?q=London,uk

    Given this information, you can send a request to the API like the example given: api.openweathermap.org/data/2.5/weather?q=London

    The response should include the necessary data to make a subsequent call for the weather forecast.

    No need to include 20MB of noise in your app bundle.