0

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.

jimmym715
  • 1,512
  • 1
  • 16
  • 25
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153

2 Answers2

1

That isn't a good strategy because you can receive multiple asynchronous location events before the first call to [manager stopUpdatingLocation]. That will lead to exponential number of timers getting created.

Instead, just start the repeating timer after creating your location manager and still stop the location manager after each received event.

ghostfly
  • 728
  • 5
  • 12
  • Thanks for your suggestion. I already took off that Timer logic, because the problem you pointed raised there and I changed my logic. Thanks again.. – Adil Soomro Apr 04 '12 at 05:07
1

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.

rckoenes
  • 69,092
  • 8
  • 134
  • 166