사용자가 지정한 간격으로 위치 업데이트가 필요한 iPhone 앱에서 작업 중입니다.그런 접근 방식을 사용하는 NSTimer와 함께 CLLocationManager의 부작용이 있습니까?
@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
코드는 마치 마법처럼 작동 : 는 여기에 내가이 일을 사용하고있는 코드 예제입니다. 나는 NSTimer
와 함께 CLLocationManager
을 사용하고
이 효과 메모리 또는 배터리 소비를 수행합니다
질문은? 사용자 환경에 부정적인 영향을 미칩니 까?
그렇다면 최적화를 통해 이러한 작업을 수행하는 링크의 도움을 받으십시오.
참고 :Utils
은 데이터를 저장하거나 검색하는 클래스입니다.
out of curiosty .. 어디에서 타이머를 startLocationManager ..로 전달합니까? –
start location manger는 nstimer를 인수로 사용하지만 통과 된 userinfo는 nil이므로 아무런 인수도 보내지 않습니다. –
타이머 자체는 iOS 프레임 워크에 의해 자동으로 전달됩니다. –