Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jeanne Merle
Jeanne Merle
3,390 Points

how to know when startUpdatingLocation is finished

Hi, I want to update labels and start using functions according to the country the user is located in, and I use the method startUpdatingLocation. But I have noticed that I use my functions too soon before the localization is completed. So some of my input parameters are empty, and so the function returns nothing.

Here's my code : override func viewDidLoad() { super.viewDidLoad() ... locationManager.startUpdatingLocation()

    //*** some parsings

.... let Fruits = CatalogueDesFruits.listeFruits (jsonFruitsResult, dictionnaireDesPays: jsonPaysResult, pays: monPaysISO, mois: 0) println("--") println(Fruits) println("--") ... }

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in

        // Il y a une erreur dans la récupération des coordonnées
        if error != nil {
            println("Error :" + error.localizedDescription)
            return
        }

        // Récupération de l'endroit en fonction de la localisation
        if placemarks.count > 0 {
            // Dans le CLPlacemark il y a aussi le PAYS !!
            let pm = placemarks[0] as! CLPlacemark
            // On affiche les informations sur l'endroit
            self.displayLocationInfo(pm)
        }
        else {
            println("Error with data")
        }
    })
}

//************************************************************************** func displayLocationInfo (pm: CLPlacemark) { // On arrête le rafraichissement de la position self.locationManager.stopUpdatingLocation()

    // On affiche les infos sur la localisation
    self.pays.text = pm.country
    self.ville.text = pm.locality

    // On initialise le nom Anglais du pays
    self.monPaysISO = pm.ISOcountryCode
}

my list "Fruits" is empty, because parameter "monPaysISO" is not updated before the call of CatalogueDesFruits.listeFruits function.

How can I be sure that self.monPaysISO is updated THEN I can call CatalogueDesFruits.listeFruits function ?

Thank you,

1 Answer

Joshua C
Joshua C
51,696 Points

Location is a funny thing in iOS. First of all, make sure you are testing location on an iOS device and not the simulator because the simulator is very buggy when it comes to location.

Your map view controller class must conform to the MKMapViewDelegate and CLLocationManagerDelegate protocols. Then, you must request permission to view location before being able to see the location by calling the "requestWhenInUseAuthorization" method on your CLLocationManager property, and lastly, you need to set the "showsUserLocation" property of your MKMapView to 'true'.

I hope these tips help!