I would like to ask for advice on stopping a CLLocationManager -startUpdatingLocation. Currently I am considering two methods, but I am unsure which to use and would like to get an idea how other folks do this:
Method_001:
[locationManager startUpdatingLocation];
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"TimedOut" afterDelay:30];
- Potentially wastes battery life as always runs for 30secs
- If network is slow might not get an accurate location in time
- Feels quite a tidy way to implement the timeout.
Method_002:
[locationManager startUpdatingLocation];
Then inside: -locationManager:didUpdateToLocation:fromLocation: add:
static int timeOut = 0;
timeOut++;
// Other code that checks for and stops
// when a suitable result, accuracy, age etc is found.
if(timeOut >= 4) {
[[self locationManager] stopUpdatingLocation];
timeOut = 0;
return;
}
- Might not resolve an accurate location in 4 (or less) attempts.
- 4 results might not be returned for CLLocationManager and we never timeout.
- Better on battery life as we stop immediately on a good result.
Just curious?
Hmm, I think I prefer the first one. I don’t know if we can be sure about how often the
didUdpateToLocation:method gets called. I think the time out is more reliable.