I'm currently building a weather application where I have one container ViewController
managing multiple child ViewControllers
, whose main views live inside the container ViewController
's UIScrollView
.
Each child VC has a location property, which is the class that holds all the weather information. This class has the following function for loading the data from the OpenWeatherMaps API:
func refresh(completionHandler: () -> ()) {
let session = NSURLSession.sharedSession()
session.dataTaskWithURL(self._apiUrl) { (responseData: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
if let data = responseData {
do {
let json: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
// parse json
completionHandler()
} catch let err as NSError {
print(err.debugDescription)
}
}
}.resume()
}
Then the child VC has a function calling the other function with the completionHandler running on the main thread (because I'm updating the user interface):
func refreshWeatherData() {
location.refresh {
dispatch_async(dispatch_get_main_queue(), {
print("\(self.location.name)")
self.forecastCollectionView.reloadData()
})
}
}
And finally from the ContainerVC I'm calling the refreshWeatherData
function on all my LocationVCs.
override func viewDidLoad() {
super.viewDidLoad()
let zurich = WALocation(name: "Zürich", id: "7287650", country: "CH", longitude: 8.53071)
let shanghai = WALocation(name: "Shanghai", id: "1796236", country: "CN", longitude: 121.45)
let boston = WALocation(name: "Boston", id: "4183849", country: "US", longitude: -83.78)
let vancouver = WALocation(name: "Vancouver", id: "6173331", country: "CA", longitude: -123.11)
addLocationControllerForLocation(shanghai)
locationControllers[0].refreshWeatherData()
addLocationControllerForLocation(boston)
locationControllers[1].refreshWeatherData()
addLocationControllerForLocation(zurich)
locationControllers[2].refreshWeatherData()
addLocationControllerForLocation(vancouver)
locationControllers[3].refreshWeatherData()
}
Now the issue I encounter is that sometimes (not always), one two or three times, the JSONSerialisation throws an error (in the refresh function of the location):
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo ``{NSDebugDescription=No value.}
When instead in my ContainerVC I do this:
override func viewDidLoad() {
super.viewDidLoad()
let zurich = WALocation(name: "Zürich", id: "7287650", country: "CH", longitude: 8.53071)
let shanghai = WALocation(name: "Shanghai", id: "1796236", country: "CN", longitude: 121.45)
let boston = WALocation(name: "Boston", id: "4183849", country: "US", longitude: -83.78)
let vancouver = WALocation(name: "Vancouver", id: "6173331", country: "CA", longitude: -123.11)
addLocationControllerForLocation(shanghai)
locationControllers[0].refreshWeatherData()
sleep(1)
addLocationControllerForLocation(boston)
locationControllers[1].refreshWeatherData()
sleep(1)
addLocationControllerForLocation(zurich)
locationControllers[2].refreshWeatherData()
sleep(1)
addLocationControllerForLocation(vancouver)
locationControllers[3].refreshWeatherData()
}
NSJSONSerialisation never fails. This makes me think it only fails when multiple requests are running asynchronously.
Any help would be greatly appreciated.