开发者

Stop Core Location updates then restart them with a timer

开发者 https://www.devze.com 2022-12-27 18:10 出处:网络
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开发者_JAVA百科.

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开发者_JAVA百科.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号