samedi 25 juin 2016

Setting Property Causes EXC_BAD_ACCESS crash (CLLocationManager Singleton)

I been banging my head at this for the past few days and it beginning to slowly drain me. I am trying to make CLLocationManager Singleton as I need a location for two other components of my app. However, I keep getting EXC_BAD_ACCESS when I go set the currentLocation property. When I do get past it, I get a SIGABRT on main.m Oh and to top it all off, when does get past, along with the SIGABRT I am informed that my KVO message was received but not handled and I have no idea what that means. Any help is appreciated along with resources on KVO and/or resources to basics I should know before using KVO LocationFetch.h @interface LocationFetch : NSObject <CLLocationManagerDelegate> +(LocationFetch *) sharedInstance; @property (nonatomic,strong) CLLocationManager *locationManager; @property (nonatomic, strong) CLLocation *currentLocation; -(void)startingUpdatingLocation; -(void)stopUpdatingLocation; @end LocationFetch.m #import "LocationFetch.h" @implementation LocationFetch @synthesize locationManager, currentLocation; +(LocationFetch *) sharedInstance { static LocationFetch *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc]init]; }); return instance; } - (id)init { self = [super init]; if(self) { self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 100; self.locationManager.delegate = self; //Request Authorization if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } } return self; } - (void)startingUpdatingLocation { [self.locationManager startUpdatingLocation]; NSLog(@"Starting Location Updates"); } -(void)stopUpdatingLocation { [self.locationManager stopUpdatingLocation]; NSLog(@"Stopping Location Updates"); } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Location updates failed with %@", error); } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { CLLocation *location = [locations lastObject]; NSLog(@"LocationFetch sharedInstance: Latitude %+.6f, Longitude %+.6fn", location.coordinate.latitude, location.coordinate.longitude); self.currentLocation = location; } @end part of WeatherFetch.m (this is where the observer is being registered) -(id)init { self = [super init]; if(self) { [[LocationFetch sharedInstance] addObserver:self forKeyPath:@"currentLocation" options:NSKeyValueObservingOptionNew context:nil]; } return self; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"currentLocation"]) { [self setWeatherLocation]; [self sendWeatherRequest]; NSLog(@"Observer has received message"); [self removeObserver:self forKeyPath:@"currentLocation" context:nil]; } } EDIT: This was the guide I was following for the Singleton : http://derpturkey.com/cllocationmanager-singleton/

Aucun commentaire:

Enregistrer un commentaire