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

Thomas Nilsen
Thomas Nilsen
14,957 Points

call CLLoationManager and it's delegates from a model

I'm trying to put everything location related inside a model. When I call this my MainViewController, the simulator doesn't ask me to my your location, and nothing happens.

When I use the same code from my model, but put it directly in ViewDidLoad in my ViewController, everything works. I'm having a hard time understanding why.

Here is my model:

@implementation Location
{
    CLLocationManager *_locationManager;
    CLLocation *_location;
}

- (void)startLocationManager
{
    NSLog(@"In startLocationManager");
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    _locationManager.delegate = self;

    [_locationManager startUpdatingLocation];
}

#pragma mark - LocationManager Delegates

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"In didUpdateLocations");

    if (locations) {
        _location = [locations lastObject];
        NSLog(@"%@", _location);
    }

}

@end

I call this in my MainViewController like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    Location *location = [[Location alloc] init];
    [location startLocationManager];
}

Why does the code work like a charm directly in the viewController, but not through my model? Amit Bijlani and Ben Jakuben I'd love an expert opinion

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

You need to make location a property of your view controller instead of a local variable in the viewDidLoad. Otherwise it is created and deallocated within that method. You need it to live through the lifecycle of your view controller.

Thomas Nilsen
Thomas Nilsen
14,957 Points

Brilliant - That was it. Thank you!

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Seems like it should work on the surface. With viewDidLoad as you've shown it here, do you get your "In startLocationManager" message in the log?

Thomas Nilsen
Thomas Nilsen
14,957 Points

Yes I do, but that's the only one

Thomas Nilsen
Thomas Nilsen
14,957 Points

This was a shorter version of a question of mine when I was debugging the code. Now that this is solved I'm still having issues with this: https://teamtreehouse.com/forum/using-delegates-in-models-to-pass-information-between-them

Thomas Nilsen
Thomas Nilsen
14,957 Points

Never mind :) Figured that one out ;)