Ios didUpdateLocations每秒都会调用延迟更新

Ios didUpdateLocations每秒都会调用延迟更新,ios,cllocationmanager,Ios,Cllocationmanager,我正在尝试实施延迟位置更新,以获得更好的电池消耗。 我将这样启动我的位置管理器: - (void)initCoreLocation { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.distanceFilter = kCLDistanceFilterNone; self.locat

我正在尝试实施延迟位置更新,以获得更好的电池消耗。 我将这样启动我的位置管理器:

- (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秒记录一次
didfishdeferredupdates
log,但是
didfupdatelocations
每秒都会不断呼叫,排除任何优化电池消耗的尝试。是否假设位置经理每30秒调用一次
didUpdateLocations

我相信您一次只需要其中一个,在
applicationIdentinterbackground
applicationWillEnterForeground中更改调用

- (**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];
}

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

从iOS开发者库:

如果您的应用程序位于前台,位置管理器不会延迟事件的传递,但会监控指定的条件。如果您的应用程序在满足条件之前移动到后台,位置管理器可能会开始延迟事件的传递

你在调试吗

如回答中所述:

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


我们需要
startUpdatingLocation
使延迟的位置更新生效。删除行
self.deferringUpdates=NO
问题是关于延迟的位置更新,而不是重大的位置更改。SLC不使用GPS,也没有任何地方可以延迟定位。它使用wifi和基站三角测量来监控重大变化。至少根据我的经验,SLC和DLU不能很好地协同工作。