خرید بک لینک

Vote count: 0

I try to download json data and image in dataTaskwithURL. The below code will get download all the image, but can't get all json datas. If I remove the completed handler(completed()) to somewhere then image will not be completed. How do I wait for image to download complete and continue on parsing json data?

Thank You

func downloadData (completed: DownloadComplete) {

    dataRequests(_weatherURL) { (jsonData) in
        if let cityData = jsonData["city"] as?  [String: AnyObject] {
            if let cityName = cityData["name"] as? String {
                self._cityName = cityName
                print(cityName)
            }
        }

        if let lists = jsonData["list"] as? [[String: AnyObject]] {
            for list in lists {
                if let dateOfNumber = list["dt"] as? Int {
                    let date = convertToHour(Double(dateOfNumber))
                    self._dictData.updateValue(date, forKey: TIME)
                }

                if let main = list["main"] as? [String: AnyObject] {
                    if let deg = main["temp"] as? Double {
                        self._degree = deg
                        self._dictData.updateValue(convertDegreeTemp(deg), forKey: DEGREE)
                    }

                    if let humidity = main["humidity"] as? Int {
                        let humidityPercent = (Double(humidity) / self._degree) * 100.0
                        self._dictData.updateValue("(round(humidityPercent))%", forKey: HUMIDITY)
                    }
                }

                if let weatherArray = list["weather"] as? [[String:AnyObject]] {
                    for weather in weatherArray {
                        if let desc = weather["description"] as? String {
                        self._dictData.updateValue(desc, forKey: DESCRIPTION)
                    }

                    if let icon = weather["icon"] as? String {
                        let urlString = URL_ICON + icon + ".png"

                        if let url = NSURL(string: urlString) {
                            let session = NSURLSession.sharedSession()

                            session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) in
                                if let imageData = data {
                                    self._weatherImg.append(imageData)
                                }
                                else {
                                    print("Could not get Image Data")
                                }
                                completed()
                            }).resume()
                        }
                    }
                }

             }

                if let wind = list["wind"] as? [String: AnyObject] {
                    if let speed = wind["speed"] as? Double {
                        self._dictData.updateValue("(round(speed * MPH))MPH", forKey: WIND)
                    }
                }

            }
            self._weatherData.append(self._dictData)
         //   completed()
        }

      //  completed()

    }
}

asked 36 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 29 فروردين 1395 ساعت: 1:07

صفحه بندی