0

我正在开发一个 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

代码就像一个魅力。

问题是:

CLLocationManager和 一起使用NSTimer,这会影响内存电池消耗吗?我的意思是对用户体验有负面影响吗?

如果是这样,任何建议,帮助链接通过优化完成此类任务将不胜感激。

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

4

2 回答 2

1

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

于 2011-12-02T11:13:48.363 回答
1

这不是一个好的策略,因为您可以在第一次调用[manager stopUpdatingLocation]. 这将导致创建指数级的计时器。

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

于 2012-04-03T23:01:25.157 回答