I’ve tried to implement the following with my locationManager:
STARTING TO UPDATE THE USERS LOCATION
- (void)startStandardUpdates {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Set a movement threshold for new events
self.locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
CLLocation *currentLocation = self.locationManager.location;
if (currentLocation) {
self.currentLocation = currentLocation;
}
}
IMPLEMENTING A CUSTOM SETTER FOR MY PROPERTY TO SEND NOTIFICATIONS
- (void)setCurrentLocation:(CLLocation *)currentLocation {
self.currentLocation = currentLocation;
NSLog(@"%f", currentLocation.coordinate.latitude);
//Notify the app of the location change
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.currentLocation forKey:kIFLocationKey];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:kIFLocationChangeNotification object:nil userInfo:userInfo];
});
}
THE PROBLEM IS:
when I run the app, I get a “BAD_EXEC (code 2)” error message in the “setCurrentLocation” method in the debug mode and the app has hung up. But I don’t get the issue. Did I miss something out? As far as I see it, in “startStandardUpdates”, if the location manager has found a user’s location, the property “currentLocation” is being updated, using my custom setter “self.currentLocation = currentLocation”.
Thanks for your help in advance!
regards
Sebastian
The way you implemented the setter is the problem. self.currentLocation = … causes the setter to be called and you call it while implementing the setter, which is causing an infinite loop. Synthesize your ivar as below, and use _variablename only in the setters (and getters).
@synthesize currentLocation = _currentLocation;
//Setter