DidUpdateLocations keeps ringing every second with pending updates

I am trying to implement pending location updates in order to have better battery consumption. I start my location manager as follows:

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

And starting a delayed update, for example, as follows:

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

I have a didFinishDeferredUpdateslog every 30 seconds, however it didUpdateLocationskeeps ringing every second, removing any attempt to optimize battery consumption. Is the location manager supposed to call didUpdateLocationsevery 30 seconds?

+4
source share
3 answers

, , allowDeferredLocationUpdatesUntilTraveled() GPS , .

iOS:

, , . , , .

0

?

: ios

, . , Xcode , , .

0

I believe that you only need one of them at a time, change the call to applicationDidEnterBackgroundandapplicationWillEnterForeground

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

Source: https://habr.com/ru/post/1616692/


All Articles