CLLocationManager running in the background and energy saving

I am working on an iOS tracer. It should start and receive places, even if the application is not in the foreground, that is, I use the background "Location Updates". However, if possible, it would be safe to charge the battery. In particular, there is no need to get seats if the device does not move.

  • I tried to install the distanceFilter instance on the CLLocationManager , but it does not save energy, it just reduces the number of location updates.
  • I cannot stop and start the location manager manually since the application will be paused if in the background.
  • I tried using the location manager with pausesLocationUpdatesAutomatically set to YES (it is turned on by deafult), but if the application is in the background and location updates are paused, the application pauses and does not wake up even if the device starts moving again.

Is there a way to save the battery when I need to find a place in the background? The pausesLocationUpdatesAutomatically flag pausesLocationUpdatesAutomatically very close to what I'm looking for, but pausing the application in the background is a show stopper for me.

+6
source share
2 answers

What are you looking for this

 - allowDeferredLocationUpdatesUntilTraveled:timeout: 

If your application is in the background and the system can optimize its energy use, the location manager tells the GPS to store new locations inside until the specified distance or timeout conditions are met. When one or both of the criteria is met, the location manager ends the pending places by calling locationManager: didFinishDeferredUpdatesWithError: delegate its method and deliver cached locations to the locationManager: didUpdateLocations: method.

ex;

 [locationManager allowDeferredLocationUpdatesUntilTraveled:100.0f timeout:CLTimeIntervalMax]; 

So basically, this will save some processing power by sending location updates as a collection of location after a certain time, rather than activating a location update callback every time the device registers movement.

And you can get the location update using the follwing callback method;

 -(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error 
+5
source

Using β€œpending location updates,” as mentioned in Laky, is perhaps the only possible way to save power for an application that must run in the background and cannot be paused. I just want to generalize what I learned by experimenting with this function:

  • Pausing the application may be prohibited, i.e. The pausesLocationUpdatesAutomatically property can be set to NO in the CLLocationManager instance, and pending location updates will work anyway.

  • When allowDeferredLocationUpdatesUntilTraveled: timeout: called, some distance and some timeout should be specified as parameters. If you provide too low values, the function will not take effect. The waiting time of 80 seconds is too low, 90 seconds is OK. The distance of 90 meters is too low, 100 meters in order. (tested on iPhone 5, iOS 8.4.1)

  • I am not sure if this function takes effect if the application is in the background. I have not watched this yet. However, it can take effect if the screen is locked.

  • The function does not take effect immediately after locking the screen. You have to wait a while. It was 30 to 150 seconds in my observations.

  • The system sometimes does not deliver location updates in anaway packages. My test application ran for 13 hours in the background with a locked screen, and 38% of this time got seats one by one. Only the remaining 62% of the lead time was received by the lot of places (collection with at least two elements). The device did not move at all.

  • The function does not take effect if the device is connected via a docking cable to the Mac and the application runs in the debugger.

  • The function is not supported by all devices, for example. It is not supported by iPhone 4 or iPad 2.

  • I took several measurements to check how much energy was saved. I run the application on iPhone 5, iOS 8.4.1 in the background with a locked screen. The application simply launches the CLLocationManager and stores statistics about the received location data (using NSUSerDefaults ) so that you can view it the next time you start the application. Wi-Fi connection is disconnected, cellular data is on. The battery was completely hacked, there were no other applications. The device did not move, and it was laid in place with a GPS signal. With pending location updates (with a minimum distance of 900 meters and a timeout of 90 seconds), the battery was completely discharged in 15.25 hours. Without pending location updates, it took 13 hours.

+13
source

All Articles