how to handle the data downloading in background dispatch queue from server if battery drains?

152 views Asked by At

I am downloading the data at background from server in an offline app, where the data will be downloaded when the app launches for the first time .Later the app will be used as offline app.

I need to place the condition to check whether the data couldn't be downloaded due to low battery, Network issues etc.

Where Can I handle these in app life cycle?

1

There are 1 answers

0
l0gg3r On

You can check [[UIDevice currentDevice] batteryState] if device is charging

UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged,   // on battery, discharging
UIDeviceBatteryStateCharging,    // plugged in, less than 100%
UIDeviceBatteryStateFull,        // plugged in, at 100%

Also you can get battery level via [[UIDevice currentDevice] batteryLevel] and decide to download content or not.

Note that batteryLevel is not so accurate, it rounded to 0.5
If you need more accurate measurements have a look https://github.com/nst/BatteryChart