我正在开发一个 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
是我的类来存储或检索数据。