Search code examples
iphonenstimercllocationmanagerpower-managementmemory-consumption

Any side effect of CLLocationManager with NSTimer using such approach?


I am working on an iPhone app which needs Location Updates with intervals specified by the user. Here is the code sample, which I am using to do this:

@implementation TestLocation
- (void)viewDidLoad{
    if ([Utils getDataWithKey:TIMER_INTERVAL] == nil) {
        [Utils saveDataWithKey:TIMER_INTERVAL withValue:@"60.0"];
    }
    locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}
- (void)startLocationManager:(NSTimer *)timer{  
    [locationManager startUpdatingLocation];
    [timer invalidate];
    timer = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // Here is code to handle location updates... 
    [manager stopUpdatingLocation];

    // Timer will start getting updated location.
    NSTimeInterval timeInterval = [[Utils getDataWithKey:TIMER_INTERVAL] doubleValue];
    [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                     target:self
                                   selector:@selector(startLocationManager:)
                                   userInfo:nil
                                    repeats:NO];

}
// other implementations ...
@end

The code is working like a charm.

The question is:

I'm using CLLocationManager along with NSTimer, does this effect memory or battery consumption? I mean any negative effect on User Experience?

If so, any suggestion, helping links to do such task with optimization will be much appreciated.

Note: Utils is my class to store or retrieve data.


Solution

  • Yes this will have some side effects, you will not get the desired accuracy. Since it will call the locationManager:didUpdateToLocation:fromLocation: every time the GPS signal will be come more accurate.