didUpdateLocations保持每秒调用一次延迟更新



我正在尝试实现延迟的位置更新,以获得更好的电池消耗。我开始这样我的位置经理:

- (void)initCoreLocation
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.pausesLocationUpdatesAutomatically = YES;
    self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //Très important pour iOS9 !
    if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
        self.locationManager.allowsBackgroundLocationUpdates=YES;
    }
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];
}

并以这种方式启动延迟更新:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (!self.deferringUpdates) {
        [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:30];
        self.deferringUpdates = YES;
    }
}
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error { // Stop deferring updates
    if(error) {
        NSLog(@"error");
    }
    NSLog(@"didFinishDeferredUpdates");
    self.deferringUpdates = NO;
}

我每30秒有一次didFinishDeferredUpdates日志,但didUpdateLocations每秒都会调用一次,消除了任何优化电池消耗的尝试。是否假设位置管理器每30秒呼叫didUpdateLocations

也许您没有正确的方法,因为allowDeferredLocationUpdatesUntilTraveled()告诉GPS硬件在内部存储新位置,直到满足指定的距离或超时条件。

来自iOS开发者库:

如果你的应用程序在前台,位置管理器不会推迟事件的交付,但会监控指定的条件。如果您的应用程序在满足标准之前移动到后台,位置经理可能会开始推迟活动的交付。链接

您正在调试吗?

如这里的答案所述:ios延迟的位置更新无法延迟

只有当系统进入低功率状态时,才会传递延迟更新。在调试期间不会发生延迟更新,因为Xcode会阻止应用程序休眠,从而阻止系统进入低功耗状态。

我相信您一次只需要其中一个,请更改applicationDidEnterBackgroundapplicationWillEnterForeground 中的调用

- (**void)applicationDidEnterBackground:(UIApplication *)application {
    // Need to stop regular updates first
    [self.locationManager stopUpdatingLocation];
    // Only monitor significant changes
    [self.locationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [self.locationManager stopMonitoringSignificantLocationChanges];
    [self.locationManager startUpdatingLocation];
}

最新更新