iOS Battery Life Performance (Part 1)
Make Battery Life Achieve All Day With using Less Energy
Energy has two components, power and time.
Energy = Power * Time.
more energy = more battery consumption.
what consumes energy?
processing, networking, location, and finally graphics. These are the four main things we want to focus on but there are some extra like (Accessories - ex: Bluetooth-, Multimedia, Camera).
1. Graphics
In the graphics, we combine with CPU and GPU so there are some tips for reducing Energy For graphics.
Animation
- Make simple animations. Complex animations consume more energy while rendering.
Display
- Reduce stacking a large number of views. Simplify the view hierarchy.
- Avoid refreshing UI when it not necessary, be sure you are updating UI when it’s important to the user. So don’t make components like (CollectionView || TableView) reload when it’s not necessary.
- Be careful when applying blur effects. Try to avoid blurring over changing content. The same for transparency; as it needs more energy for rendering back views changes and blur.
- Average Pixel Luminance (APL): On OLED devices like iPhone X, UIColor needs an amount of energy to display. So lighter colors have more APL, so they consume more energy. Darker colors have lower APL and thus consume less energy.
In Games
- Be careful with frame rates. Avoid mixing multiple frame rates in one screen like in action games, don’t jump at 60 FPS, and move the sword at 30 FPS.
Finally, a good rule of thumb is to say that the more rendering that your app does, doing animations or UI, the more energy you’re going to consume in the form of graphics.
2. Location
We have to turn on more hardware such as GPS, Wi-Fi, and cellular, talk to satellites All time. the precise location is the most expensive. Imprecise location is a lot cheaper, try to avoid a leak in the location subsystem. and in future Parts, we will learn how to debug and use tools to track the leak for location and peak energy for the app.
- Lower accuracy is better for energy. Only use the best accuracy if you really need it.
- When you start requesting location service, don’t let it live in the app all the time. Instead, make it live for a short period, and close it as soon as possible. ex: your feed timeline Constructively with a location, you get location and end tracking of location and don’t open it again unless you really need it. the more time spent tracking location in your application, energy is consumed.
- if you allowed your app to track location in the background and enabled “allowsBackgroundLocationUpdates” you can use “pausesLocationUpdatesAutomatically” to stop updating location and save battery if it’s not necessary to update and you can support this with “activityType”.
- If GPS-level accuracy isn’t critical for your app, you don’t need continuous tracking, and region or visit monitoring isn’t more appropriate for your app, you can use the significant-change location service instead of the standard one. so use “startMonitoringSignificantLocationChanges” and “stopMonitoringSignificantLocationChanges”.
Region and visit monitoring are sufficient for most use cases and should always be considered before significant-change location updates. Significant-change location updates run continuously, around the clock, until you stop them, and can actually result in higher energy use if not employed effectively.
- Defer location updates when running in the background. Location updates are a way for the location manager to avoid frequently waking up a background app to deliver location changes. Normally, when an app wants location updates in the background, the app must be woken up whenever a new event arrives. Waking up the app consumes power, which in some situations might be wasted if the app cannot do anything with the location information other than logging it and go back to sleep anyway. Deferring location updates gives you the ability to wait until a time when your app can do something useful with the data and then process the updates all at once. Deferred location updates require the presence of GPS hardware and may not be supported on all iOS devices.
Article segmentation
part1: Tips for graphic and location.
part2: Tips for Networking and Processing.
Part3 & 4: debugging energy, leaks, And battery matrices.
Reference:
- WWDC.