2015-02-23 85 views
3

我傳遞一組經緯度座標以進行反向地理編碼。反向地理編碼的工作原理和我取回我的地址數據,但我需要從這些數據創建一個URL,我進一步下去。我發現在恢復數據之前NSURLSession正在執行。你可以看到我在println中顯示「第一個」和「第二個」的位置,但在執行此代碼時,在輸出控制檯中顯示「second」,然後顯示「first」和地址數據,米沒有按順序運行。在Swift中同步代碼執行

如何強制執行的順序,需要我的變量addchunk在NSURLSession運行之前存在?

謝謝!

@IBAction func grabData(sender: UIButton) { 
    var latitude:CLLocationDegrees = (latitudeEntry.text as NSString).doubleValue 
    var longitude:CLLocationDegrees = (longitudeEntry.text as NSString).doubleValue 
    var location = CLLocation(latitude: latitude, longitude: longitude) 

    // reverse geocodes the supplied latitude and longitude 
    CLGeocoder().reverseGeocodeLocation(location, completionHandler: { 
     (placemarks, error) -> Void in 
     if error != nil { 
      println("Reverse geocoder failed with error" + error.localizedDescription) 
     } 
     if placemarks.count > 0 { 
      let pm = placemarks[0] as CLPlacemark 
      self.addchunk = "\(pm.subThoroughfare)-\(pm.thoroughfare),-\(pm.locality),-\(pm.administrativeArea)".lowercaseString 
      println("first " + self.addchunk) 
     } else { 
      println("Problem with the data received from geocoder") 
     } 
    }) 

    var url = NSURL(string: "http://www.example.com/" + addchunk) 
    println("second " + addchunk) 
    if url != nil { 
     let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { 
      (data, response, error) -> Void in 
    ... 
+0

爲什麼不改變你的網址代碼到reverseGeocodeLocation的完成處理程序中? – chris 2015-02-23 04:40:55

+0

是的,從'completionHandler'中調用這段代碼。我們可以告訴你如何同步進行(我打賭有人會這樣做),但這是一種可怕的模式。永遠不要阻塞主隊列。調用依賴於'completionHandler'內的'reverseGeocodeLocation'結果的代碼。 – Rob 2015-02-23 04:45:57

+0

謝謝,Chris和Rob!關於不阻塞主隊列的好處。 – 2015-02-23 05:17:41

回答

2

更新你這樣的代碼:

var latitude:CLLocationDegrees = ("-34.4232722" as NSString).doubleValue 
    var longitude:CLLocationDegrees = ("150.8865837" as NSString).doubleValue 
    var location = CLLocation(latitude: latitude, longitude: longitude) 

    // reverse geocodes the supplied latitude and longitude 
    CLGeocoder().reverseGeocodeLocation(location, completionHandler: { 
     (placemarks, error) -> Void in 
     if error != nil { 
      println("Reverse geocoder failed with error" + error.localizedDescription) 
     } 
     if placemarks.count > 0 { 
      let pm = placemarks[0] as CLPlacemark 
      self.addchunk = "\(pm.subThoroughfare)-\(pm.thoroughfare),-\(pm.locality),-\(pm.administrativeArea)".lowercaseString 
      println("first " + self.addchunk) 
     } else { 
      println("Problem with the data received from geocoder") 
     } 
     var url = NSURL(string: "http://www.example.com/" + self.addchunk) 
     println("second " + self.addchunk) 
     if url != nil { 
      let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { 
       (data, response, error) -> Void in 
      }) 
     } 
    }) 

我測試了這一點,其工作fine.and這是我的座標輸出:

first 18-hercules street,-wollongong,-nsw 
second 18-hercules street,-wollongong,-nsw