iOS Battery Life Performance (Part 2)

Abdelrahman mohsen
4 min readMar 25, 2021

--

Make Battery Life Achieve All Day With using Less Energy

3. Networking

Almost all iOS apps perform network operations of some kind, and it’s essential that networking be employed efficiently. We want to optimize networking because it is a high-energy subsystem.

  • Connectivity on your network -if the connection is Poor or Satisfied-. It plays a huge role in the amount of energy that you consume. because Poor connection takes a long time to finish Transactions. We have debugging metrics for the network.
  • The cellular network requires more energy than Wi-Fi. There are several options to check networking such as “NWPathMonitor” or Reachability.
import Networklet monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.usesInterfaceType(.wifi) {
print("Wi-Fi")
} else if path.usesInterfaceType(.cellular) {
print("Cellular")
}
//or check for Connectivity for network
if path.status == .satisfied {
print("We're connected!")
} else {
print("No connection.")
}

//whenever connection status changed the closure will be called
}
  • If your app streams video or downloads large files try to avoid cellular and use Wi-Fi to down crease energy.
  • If you want to connect your App analytics with the server use Background Session. This will really help battery life for your users.
  • If you want to upload an image. limit retries max 3 times and set time out about 1 minute, Don’t spend too much time sending data, and maybe you have a poor connection. Finally, if your retry limit is hit, and you’re not sure what to do, maybe create a background session.
  • Don’t use a timer to get new data from the server in your app timeline. Reload only when needed as user interaction or notification is fired.
  • Compress data before sending & receiving.
  • You shouldn't download the same data. use NSURL Session Cache. This will allow you to not re-download the same pieces of your data again.

4. Processor

It’s going to be the energy consumed when your app system resources are on. Energy consumed here is going to be highly dependent on the code that your app is executing and the workload that you’ve asked your app to perform. the more operations and code your app executes, the more energy it will consume in the form of processing.

  • Start use processing metrics-will explain it in the metrics part-. you can find unexpected rendering in your application using these metrics. or comparing the algorithmic efficiency of your features. feature A and feature B, you can use these metrics to determine which one is better for battery life.
  • Avoid timers whenever possible. Try to do your work based on user interaction. if you really need Timer, Use timers economically by specifying suitable timeouts. Invalidate repeating timers when they’re no longer needed. Set tolerances for when timers should fire.
  • If you want to update your content, try to use the background app refresh API.

background processing can be very expensive for your users and so it’s important to think about what you’re doing in the background.

  • Make sure that you use the completion handler. This will make sure that you allow the device to sleep as quickly as possible.
  • Take care if the low power of the phone is enabled to reduce activity. You can check for that.
if ProcessInfo.processInfo.isLowPowerModeEnabled {print("Low power enabled")}else{print("battery normal status")}

Your app shouldn’t wait to be suspended by the system. It should begin winding down activity immediately once notified that state has changed. When your app completes any remaining tasks, it should notify the system that background activity is complete. Failing to do so causes the app to remain active and draw energy unnecessarily.

Finally, if your App has a small Problem for any small tip for the main 4 subjects that we talked about. like leaking in location services or forget Invalidate timer, it will consume energy and battery life. and you will see the bad result in Energy Gauges.

Article segmentation

part1: Tips for graphic and location.

part2: Tips for Networking and Processing.

Part3 & 4: debugging energy, leaks, And battery matrices.

--

--

No responses yet