Ray
Ray

Reputation: 59

Synchronous code execution in Swift

I'm passing a set of latitude and longitude coordinates to be reverse geocoded. The reverse geocoding works and I get back my address data, but I then need to create a URL from that data, which I do further down. What I'm finding is that the NSURLSession is executing before I get my data back. You can see where I put in the println to display "first" and "second", but in the output console when I execute this code, "second" displays and then "first" along with the address data, which tells me I'm not running in order.

How can I force the order of the execution, requiring my variable addchunk to exist before the NSURLSession gets run?

Thanks!

@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
    ...

Upvotes: 3

Views: 926

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Update you code like this:

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
            })
        }
    })

I have tested this and its working fine.and this is the output for my coordinates:

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

Upvotes: 2

Related Questions