使用这种方法的CLLocationManager与NSTimer的任何副作用?

时间:2011-12-02 10:37:06

标签: iphone nstimer cllocationmanager power-management memory-consumption

我正在开发一款iPhone应用程序,它需要用户指定的时间间隔进行位置更新。 这是代码示例,我用它来执行此操作:

@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

代码就像魅力一样。

问题是:

我正在使用CLLocationManagerNSTimer,这会影响内存电池消费吗?我的意思是对用户体验有任何负面影响吗?

如果是这样,任何建议,帮助链接以优化执行此类任务将非常感激。

注意: Utils是我存储或检索数据的类。

2 个答案:

答案 0 :(得分:1)

是的,这会产生一些副作用,您将无法获得所需的准确度。因为每次GPS信号都会调用locationManager:didUpdateToLocation:fromLocation: 来得更准确。

答案 1 :(得分:1)

这不是一个好策略,因为您可以在第一次调用[manager stopUpdatingLocation]之前收到多个异步位置事件。这将导致创建指数数量的计时器。

相反,只需在创建位置管理器后启动重复计时器,并在每次收到事件后仍然停止位置管理器。