iOS - CLLocation Manager didUpdateLocations调用了太多时间

时间:2017-03-02 10:04:40

标签: ios objective-c geolocation cllocationmanager cllocation

我正在重构旧代码,以便让事情变得干净,尤其是地理定位部分,现在它有点乱。

我正在记录每个地理位置方法,我发现#include <ctype.h> //header for isspace while ( scanf ( "%c", &letter) == 1) { if ( isspace ( letter)) { if ( letter == '\n') { break; } } else { ungetc ( letter, stdin);//replace letter in input stream if ( scanf ( "%19s", array[i]) == 1) { i++; if ( i >= 20) { break; } } } } 被称为太多时间。

以下是我的Splashscreen的一些代码:

didUpdateLocations

以下是- (void)viewDidLoad { [super viewDidLoad]; _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; [self startLocationRetrieval]; } 方法

startLocationRetrieval

以下是CLLocation委托方法

   -(void) startLocationRetrieval
    {
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            [_locationManager requestWhenInUseAuthorization];
        if ([CLLocationManager locationServicesEnabled]) {
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            [_locationManager startUpdatingLocation];
        }else {
            _isDataLoaded = YES;
            [self loadHomeView];
        }

我处理位置变化的方法

#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]];
}

loadHomeView方法只是触发performSegueWithIdentifier以转到HomePage。

  • 您认为这是一个很好的实施还是更清洁?
  • 我应该在哪里-(void) manageNewLocation:(CLLocation *)location { _coordinate = location; DDLogDebug(@"GEOLOC Splash manageNewLocation = %@", _coordinate); [self loadHomeView]; }

我发现了类似的问题IOS didUpdateLocations。但stopUpdatingLocation正确的方式,似乎很笨拙。

1 个答案:

答案 0 :(得分:0)

提取位置后,您需要使用[manager stopUpdatingLocation];

停止更新位置
  - (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
       [manager stopUpdatingLocation];
       manager = nil;
    DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
    [self manageNewLocation:[locations lastObject]];
}