iOS - CLLocation 管理器 didUpdateLocations 调用了太多时间
iOS - CLLocation Manager didUpdateLocations called too much time
我正在重构旧代码以使事情变得干净,尤其是地理定位部分,现在有点乱。
我正在记录每个地理定位方法,我发现 didUpdateLocations
被调用的时间太多。
这是我的 Splashscreen 中的一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startLocationRetrieval];
}
这里是startLocationRetrieval
方法
-(void) startLocationRetrieval
{
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[_locationManager requestWhenInUseAuthorization];
if ([CLLocationManager locationServicesEnabled]) {
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
}else {
_isDataLoaded = YES;
[self loadHomeView];
}
这是 CLLocation 委托方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
DDLogError(@"GEOLOC CLLocationManager didFailWithError: %@", error);
[self loadHomeView];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}
以及我处理位置变化的方法
-(void) manageNewLocation:(CLLocation *)location
{
_coordinate = location;
DDLogDebug(@"GEOLOC Splash manageNewLocation = %@", _coordinate);
[self loadHomeView];
}
loadHomeView 方法只是触发performSegueWithIdentifier 以转到HomePage。
- 您认为这是一个很好的实现还是可以更简洁?
- 我应该去哪里
stopUpdatingLocation
?
我发现了这个类似的问题 IOS didUpdateLocations。但是 NSTimer
正确的方法吗,它看起来很笨重。
获取位置后,您需要使用 [manager stopUpdatingLocation];
停止更新位置
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
[manager stopUpdatingLocation];
manager = nil;
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}
我正在重构旧代码以使事情变得干净,尤其是地理定位部分,现在有点乱。
我正在记录每个地理定位方法,我发现 didUpdateLocations
被调用的时间太多。
这是我的 Splashscreen 中的一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startLocationRetrieval];
}
这里是startLocationRetrieval
方法
-(void) startLocationRetrieval
{
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[_locationManager requestWhenInUseAuthorization];
if ([CLLocationManager locationServicesEnabled]) {
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
}else {
_isDataLoaded = YES;
[self loadHomeView];
}
这是 CLLocation 委托方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
DDLogError(@"GEOLOC CLLocationManager didFailWithError: %@", error);
[self loadHomeView];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}
以及我处理位置变化的方法
-(void) manageNewLocation:(CLLocation *)location
{
_coordinate = location;
DDLogDebug(@"GEOLOC Splash manageNewLocation = %@", _coordinate);
[self loadHomeView];
}
loadHomeView 方法只是触发performSegueWithIdentifier 以转到HomePage。
- 您认为这是一个很好的实现还是可以更简洁?
- 我应该去哪里
stopUpdatingLocation
?
我发现了这个类似的问题 IOS didUpdateLocations。但是 NSTimer
正确的方法吗,它看起来很笨重。
获取位置后,您需要使用 [manager stopUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
[manager stopUpdatingLocation];
manager = nil;
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}