3

I have a GPS app that already uses a fair amount of battery. Because of the nature of the app, I don't want the user to loose all of their data if their battery dies without them knowing it. So, I figured I would monitor the battery and then save and stop the GPS data if the battery is very low. I would use:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(batteryStateDidChange:)
                                             name:UIDeviceBatteryStateDidChangeNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(batteryLevelDidChange:)
                                             name:UIDeviceBatteryLevelDidChangeNotification
                                           object:nil];

So, a few questions:

  • Would monitoring the battery cause even MORE battery drain?
  • Is it a good idea to auto-save (core-data) for the user right before the battery dies?
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

2 Answers2

4

The device already has to monitor battery notifications in order to display the battery level when the time / carrier / signal strength is visible... I wouldn't expect registering for the notifications would add any additional stress to anything (power consumption). Maybe a few extra cpu cycles from your app for handling the notification. :-)

Auto saving might be a smart idea.

But another idea might be to cease GPS / CoreLocation services for your app once battery gets underneath a certain level (or offer that as a user-settable option).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

If the iDevice dies while your application is running or while the device is "sleeping" your UIApplicationDelegate will be sent a applicationWillTerminate: message.

If the application was put in the background then you can handle the saving there as well in your appdelegate's applicationWillEnterBackground

Be careful about Autosaving it can randomly cause CPU usage causing your user interface to be jerky at supposedly "random" intervals.

Brent Priddy
  • 3,817
  • 1
  • 24
  • 16