SG1
SG1

Reputation: 2901

Core Location: toggle startMonitoringSignificantLocationChanges down from kCLDistanceFilterNone

In order to conserve battery, I monitor location updates to see if the user has been stationary for a period of time; if so, I then downgrade the CLLocationManager from its main settings of kCLLocationAccuracyBestForNavigation and kCLDistanceFilterNone (maximum settings) to monitoring significant location changes only.

The trouble is, it doesn't work: after calling startMonitoringSignificantLocationChanges, the location updates continue to pour in at a high rate as they did prior to the call.

How do you wind activity down and then back up again?

Update: this code answers the question:

        //Set
        if ( shouldMonitorSignificantChangeUpdates ) {
            NSLog(@"Entering -> significant change mode");
            [self.locationManager stopUpdatingLocation];
            [self.locationManager startMonitoringSignificantLocationChanges]; //aka stop monitoring every location change
        } else {
            NSLog(@"Exiting <- significant change mode");
            [self.locationManager stopMonitoringSignificantLocationChanges]; //aka begin monitoring every location change
            [self.locationManager startUpdatingLocation];
        }

Upvotes: 1

Views: 1188

Answers (2)

SG1
SG1

Reputation: 2901

Actually the code as edited in the question does work. It allows the system to go back and forth from "constant stream of locations" to "occasional updates" as desired.

I think my initial testing was simply not aggressive enough and the docs don't really discuss using both techniques, instead imagining an app which needs either navigation or significant changes only.

Upvotes: 1

nevan king
nevan king

Reputation: 113777

From the docs:

startMonitoringSignificantLocationChanges does not rely on the value in the distanceFilter property to generate events

Instead of using startMonitoringSignificantLocationChanges, use a timer to stop and start the location updates.

You could also try turning off location updates, then turning it back on using startMonitoringSignificantLocationChanges. Don't forget that this will make the system launch your app after it's been terminated when it detects a significant location change. It doesn't look like that's what you really want.

Upvotes: 1

Related Questions