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 Build a Weather App with Swift Refreshing the Data The End

Carson Carbery
Carson Carbery
9,876 Points

Stormy - with CoreLocation. Had to put retrieve weather into the didUpdateLocations Delegate

Hi I've done the extras suggested by Pasan to put corelocation into Stormy and got it working. However to get it working I had to put the retrieve weather method into the corelocation didUpdateLocations delegate, otherwise when it was run in viewdidload as before, the location coordinates were not available, though I called my getLocation method before. This makes it quite hard to find where retrieve weather method is being called!!

Can anyone explain, why there seems to be a time lapse before the delegate function is performed and more importantly how it would be possible to move my retrieveWeather method back to the viewDidLoad or some other more visable method. Here is my current code:

// MARK: - Location manager delegate methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last

    print("Latitude: \(location!.coordinate.latitude), Longitude: \(location!.coordinate.longitude)")

    coordinate = ((location?.coordinate.latitude)!,(location?.coordinate.longitude)!)

    print(coordinate.lat, coordinate.long)


    locationManager.stopUpdatingLocation()

    retrieveWeatherForecast()

}

I would like to be able to move retreiveWeatherForecast to somewhere more visiable e.g in viewDidLoad as before:

override func viewDidLoad() { super.viewDidLoad()

    getUserLocation()
    configureView()
    retrieveWeatherForecast()

Many thanks with any help with understanding this