0

I am refactoring old code in order to get things clean, especially the geolocation part, which is kind of messy right now.

I am logging every geolocation methods and I found out that the didUpdateLocations is called too much time.

Here is some code from my Splashscreen:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    [self startLocationRetrieval];
}

Here is the startLocationRetrieval method

   -(void) startLocationRetrieval
    {
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            [_locationManager requestWhenInUseAuthorization];
        if ([CLLocationManager locationServicesEnabled]) {
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            [_locationManager startUpdatingLocation];
        }else {
            _isDataLoaded = YES;
            [self loadHomeView];
        }

Here are the CLLocation delegate methods

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    DDLogError(@"GEOLOC CLLocationManager didFailWithError: %@", error);
    [self loadHomeView];
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
    [self manageNewLocation:[locations lastObject]];
}

And my method to handle location changes

-(void) manageNewLocation:(CLLocation *)location
{
    _coordinate = location;
    DDLogDebug(@"GEOLOC Splash manageNewLocation = %@", _coordinate);
   [self loadHomeView];
}

The loadHomeView method is simply to trigger the performSegueWithIdentifier in order to go to the HomePage.

  • Do you think this is a good implementation or can it be cleaner ?
  • Where should I stopUpdatingLocation ?

I found this similar question IOS didUpdateLocations. But does NSTimer the proper way to go, it seems clunky.

Community
  • 1
  • 1
  • Check this http://stackoverflow.com/questions/22292835/how-to-stop-multiple-times-method-calling-of-didupdatelocations-in-ios – kb920 Mar 02 '17 at 10:06

1 Answers1

0

once location is fetched you need to stop the update location using [manager stopUpdatingLocation];

  - (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
       [manager stopUpdatingLocation];
       manager = nil;
    DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
    [self manageNewLocation:[locations lastObject]];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Ok perfect, I wanted to know where exactly the stopUpdatingMethod should be called. The manager = nil is for what ? –  Mar 02 '17 at 10:12
  • 1
    @Murloc - once your location is fetched you used the `stopUpdatingMethod` it is stop for fetch the another one time , if you are using `manager = nil ` you are deallocating the memory of your `_locationManager = [[CLLocationManager alloc] init];`, in here we are taken the local/instance name of delegates `(CLLocationManager *)manager` – Anbu.Karthik Mar 02 '17 at 10:14
  • Thank you. Clean and perfect answer. –  Mar 02 '17 at 10:15