iphoneobjective-ccocoa-touchcore-locationpower-saving

Stop Core Location updates then restart them with a timer


I was wondering if anyone could point me to (or paste in) some code to deal with turning off Core Location updates to save power.

As far as I understand it, you should stop Core Location updates as soon as you get a reading of desired accuracy. If you don't get a good accuracy reading after a certain time, you should also stop updates (presumably using a timer). Every time you stop updates, you should fire a timer (around 60 seconds) to restart Core Location and get a new reading.

Is there Apple code which does all this? The LocateMe, TaggedLocations and Locations sample code don't seem to do it.


Solution

  • The LocateMe example has the code you need. You just need to create a second Selector to Fire. LocateMe calls the following in it's setup method...

        [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:[[setupInfo objectForKey:kSetupInfoKeyTimeout] doubleValue]];
    

    It says that after a certain amount of time (kSetupInfoKeyTimeout), please call the stopUpdatingLocation method the the argument of NSString = "Timed Out". Inside the stopUpdatingLocation method, the [locationManager stopUpdatingLocation] is called to tell CoreLocation to stop.

    So, all you need to do is add another Selector like this...

    [self performSelector:@selector(timeToRestartCoreLocation) afterDelay: 60];
    

    inside the stopUpdatingLocation method, which will call the timeToRestartCoreLocation method after 60 seconds. Then inside your timeToRestartCoreLocation method, call [locationManager startUpdatingLocation] to kick off CoreLocation again.