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
Brenden Konnagan
16,858 PointsCLLocation and KVO
I am working on a simple application that gathers a users current data in a location model. That is working and I can log the locations. However, no viewControllers are seeing the updated variable. I am using KVO in the viewControllers to fire but it seems that CLLocation does not send using 'NSKeyValueObservingOptionNew'. It will fire if I use the 'NSKeyValueObservingOptionInitial' but this fires before the CLLocationManager has had an opportunity to set the instance.
Does anyone know why CLLocation is not sending the "...New" value? Like I said, it is working only when it is set to the "...Initial" state.
Thoughts?
View Controller Implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (_location == nil) self.location = [[BKLocation alloc] init];
self.theMap.showsUserLocation = YES;
[self.location addObserver:self forKeyPath:@"locationUpdated" options:NSKeyValueObservingOptionNew context:NULL];
[self.location findLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:@"locationUpdated"]) {
CLLocation *locationVariable = self.location.currentLocation;
NSLog(@"the location vaiable: %@", locationVariable);
self.theMap.centerCoordinate = self.location.currentLocation.coordinate;
}
}
Location Model Implementation:
- (void)findLocation {
[self startLocationManger];
}
- (void)startLocationManger {
if ([self locationManager] == nil) self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *locationFix = [locations lastObject];
self.currentLocation = locationFix;
NSLog(@"Current Location: %@", self.currentLocation);
}
1 Answer
Brenden Konnagan
16,858 PointsSee comment for answer
Brenden Konnagan
16,858 PointsBrenden Konnagan
16,858 PointsI found the answer... when calling the addObserver method, I was setting the arguments wrong. Should be
[self addObserver: self forKeyPath:@"self.location.currentLocation" options:NSKeyValueObservingOptionNew context:NULL];