0

I'm new to swift and I'm working on a weather app that requests the data from darksky.net API, I've managed to send the request and receive the json response, however darksky returns all of the weather data (current weather, hourly weather and daily weather) in one json file response.

I'm trying to parse the data into different classes without the need to start a new request everytime. How can I achieve that?

Should this json respone be stored somewhere? Is there a way to pass it between (model)classes without starting a new request?

I have tried creating a networking class that requests the json, and stores it in an JSON object, however, whenever I start an instant of that class, it will fire the request again.

import Foundation
import Alamofire
import SwiftyJSON

class CurrentWeather {
    private var _date: Date!
    private var _currentTemp: Double!
    private var _feelsLike: Double!

    var date: Date {
        if _date == nil {
            _date = Date()
        }
        return _date
    }

    var currentTemp: Double {
        if _currentTemp == nil {
            _currentTemp = 0.0
        }
        return _currentTemp
    }
    var feelsLike: Double {
        if _feelsLike == nil {
            _feelsLike = 0.0
        }
        return _feelsLike
    }

    func getCurrentWeather(completion: @escaping(_ success: Bool) -> Void)  {
        let LOCATIONAPI_URL = "https://api.darksky.net/forecast/0123456789abcdef9876543210fedcba/42.3601,-71.0589"

        // request JSON from url, using Alamofire Library
        Alamofire.request(LOCATIONAPI_URL).responseJSON { (response) in

            let result = response.result

            // check if respose is valid
            if result.isSuccess{
                let json = JSON(result.value!)
                // if response is valid, fill in the varibles with JSON returned data
                self._date = currentDateFromUnix(unixDate: json["currently"]["time"].double)

                self._currentTemp = json["currently"]["temperature"].double
                self._feelsLike = json["currently"]["apparentTemperature"].double
                completion(true)

            } else {
                completion(false)
                print("No Result found for this location")
            }
        }
    }
}

Should this json respone be stored somewhere? Is there a way to pass it between (model)classes without starting a new request?

Olympiloutre
  • 2,268
  • 3
  • 28
  • 38
  • Yes, you can save the data and there are many many ways to go about it, and that makes this question very broad. – Shamas S Aug 26 '19 at 02:01
  • You can save your data to `UserDefaults`. You can keep your data around in a `singleton` object, every time you fetch it from the backend. You can save it to a local database. It's like I said, it's a very broad question. – Shamas S Aug 26 '19 at 02:04
  • @SShahid What would be the best way to store the data if the main goal is to use that data to fill in other model classes, for example hourly or daily that will be placed in a collectionview. A singleton would not work in that case since the user might add/change the city in which the weather is requested. – user3685876 Aug 26 '19 at 02:50
  • That is my whole point mate. That this is a very subjective question. Ideally, you should have a central co-ordinator, that should just keep the result fetched and then it should pass the result to other views. – Shamas S Aug 26 '19 at 02:55
  • @SShahid thanks, so if I create a new co-ordinator class that will fetch and keep the results just once, how will I pass it into other classes without creating an instant from it? or will this need to be stored in a database or UserDefaults like you said? Sorry bare with me, I'm still learning. Thanks – user3685876 Aug 26 '19 at 03:01

0 Answers0