ich zur Zeit den Aufbau einer Wetter-Anwendung, wo ich einen Container ViewController
Verwaltung mehrerer Kind ViewControllers
, deren Hauptansichten leben im Inneren des Behälters ViewController
‚s UIScrollView
haben.Swift NSURLSession: Probleme, wenn mehrere Anforderungen gleichzeitig laufen
Jedes untergeordnete VC verfügt über eine Standorteigenschaft. Dies ist die Klasse, die alle Wetterinformationen enthält. Diese Klasse hat die folgende Funktion für die Daten aus dem OpenWeatherMaps API Laden:
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()
}
Dann wird das Kind VC hat eine Funktion, die andere Funktion mit der completionHandler läuft auf dem Haupt-Thread aufrufen (weil ich die Benutzeroberfläche bin Aktualisierung):
func refreshWeatherData() {
location.refresh {
dispatch_async(dispatch_get_main_queue(), {
print("\(self.location.name)")
self.forecastCollectionView.reloadData()
})
}
}
Und schließlich vom ContainerVC ich rufe die refreshWeatherData
Funktion auf allen meiner 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()
}
Nun ist die Frage, die ich ist begegnen, dass manchmal (nicht immer), ein zwei- oder dreimal, die JSONSerialisation einen Fehler wirft (in der Refresh-Funktion des Ortes): Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo ``{NSDebugDescription=No value.}
Wenn stattdessen in meinem ContainerVC I tun Sie dies:
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()
}
NSJSONSerialisierung schlägt nie fehl. Dies lässt mich glauben, dass es nur fehlschlägt, wenn mehrere Anforderungen asynchron ausgeführt werden.
Jede Hilfe würde sehr geschätzt werden.
Wo (eigentlich ** wann **) speichern Sie die Wetterdaten? – vadian
Ich speichere es, wenn ich es parse, wo ich den Kommentar in den Do-Block der Refresh-Funktion – Nico