📜 ⬆️ ⬇️

How to optimize power consumption in iOS

image


Users of devices running iOS often complain that the battery is quickly discharged. When does this happen? Most often when using GPS, but there are other reasons for the accelerated discharge of the battery. For example, rich exchange through different types of transceivers (cellular, bluetooth) or intensive graphics rendering. Some lovers of apple devices sin on the battery, but often application developers are to blame for the rapid discharge. It is important for users that the charge is enough for a long time, so when developing it is worth avoiding solutions that increase power consumption. But why would developers even think about how long the phone holds a charge? The longer the iPhone is on, the greater the chance that the user will open the application and use it.


I will consider the main energy consumers and approaches that help reduce the power consumption of the device.


Main energy consumers


Most charge is spent:



How is energy consumed while doing work?


image
Fig. 1. Power consumption when performing work.


In fig. 1 shows how power is consumed when the device performs work. We also see the division into two parts: fixed and dynamic power consumption.


Fixed power - the energy that is needed to keep the system running, activate data transmitters (be it cellular, Wi-Fi, GPS), etc.


Dynamic power consumption is the amount of energy that is needed to do useful work.


Let's go through the states from fig. 1 for clarity:


  1. The system is in an idle state - minimum power consumption. Even when idle, the device consumes energy to remain responsive.
  2. Some activity occurs, for example, a PUSH notification has arrived.
  3. Simple, the power consumption has decreased after the push notifications have been processed.
  4. Again, some activity, for example, the user clicked on the notification, and the application started. It displayed full information on the notification, after which the user closed the application.
  5. Simple, power consumption again decreased. Note that this state is identical to state 3, the only difference is that after state 3, there was again an activity that increased energy consumption.
  6. Power consumption continues to decrease, and the system again enters an idle state.

What does this mean? When work needs to be done, power consumption increases rapidly to complete the task immediately. After work, it takes significantly more time to reduce energy consumption. A fall occurs in an idle state, which takes time to enter.


What to do to reduce power consumption?


1. Avoid doing work in the inactive state

If the application is not currently displayed (for example, it is blocked by another application), stop the timers, streams, work on the network, stop redrawing the screen. To do this, just subscribe to the delegates



They will be called at the moment the application transitions from the foreground and returns to it. NSNotification has corresponding notifications.


2. Do the work at the optimal time

The best moment is the one during which the minimum amount of charge will be spent on the work. The developer does not know which time point is the best for completing the task, but the system knows this, which itself can schedule the execution.
This can be useful when you need to download or send large amounts of data, periodically update information, or perform other periodic activities over the network.


For planning work on the network there is: URLSessionConfiguration.background(withIdentifier) . With it, you can create queries and process the results the next time you start the application. It also supports automatic repeat request in case of failure. When creating a task, the system chooses the most opportune moment for execution.


When to think about using the background configuration:


  1. Auto save
  2. Data processing
  3. Content Swapping
  4. Doing something with an interval of 10 minutes or more

Example:
You need to download a lot of data for further work offline. It will be possible to process the results of the requests the next time the system or user decides to continue the application, since all these requests are executed on the system thread and their results will accurately come to the application.


3. Do work more efficiently.

Suppose the work is already being done, even at the best of times. But it is also important to give this work an appropriate priority. The iOS system has system queues, and each of them has its own priority for owning resources. Thus, it is possible to regulate how much processor time and other resources will be allocated for the task.



image
Fig. 2. Power consumption in different queues.


Let's look at the power consumption when executing the same code on different queues. Dynamic power consumption will be the same, but the total will be less, because, having reduced the speed of implementation, we have reduced the fixed costs. That is, the total consumption decreased due to a decrease in fixed. More efficient code is always less energy consuming.


4. Do less work

To do this, you should improve performance in four areas:


1. CPU


First you need to determine the code that actively uses the CPU. This will help XCode Instruments. Typically, this code works with timers and calls that stop the flow: NSTimer , GCD timers , performSelector (withObject, afterDelay) , CFRunLoopTimer , pthread_cond_timedwait () , sleep () , dispatch_semaphore_wait () .


Consider this process on the example of a timer.
image
Fig. 3. Power consumption during timer operation.


We see overhead costs that do not have time to decline due to frequent timer triggers. To improve performance when using the timer, use setTolerance . This will allow the operation to occur at the optimum time selected by the system.
myTimer.setTolerance(60.0) // 60


2. Drawing graphics
To save energy, do not take unnecessary actions. For example, instead of calling setNeedsDisplay , which redraws the entire View , call setNeedsDisplayInRect . Avoid blending blurs on frequently redrawn objects, such blending will cause the blues to constantly redraw. Also avoid unnecessary redrawing that removes power from the sleep level. You can monitor drawing through Quartz Debug or Instruments.


3. Interaction with the repository



4. Signal transceivers
Different types of transmitters (mobile, Wi-Fi, Bluetooth, GPS) are often used in applications. Usually they are accessed as soon as it is initiated by the user or data has appeared for sending. Let's look at the graph of energy consumption with this approach.


image
Fig. 4. Power consumption during active network interaction with an interval.


Due to the fixed costs of transmitter activation, power consumption remains high all the time when data is sent periodically. It should also be borne in mind that energy consumption depends on the type of transceiver we use. For example, on 5S when using Wi-Fi, the battery withstands 10 hours of surfing, and with a mobile connection - 8 hours.
If possible, buffer the data and send it all at one time, as in the graph below.


image
Fig. 5. Power consumption with active network interaction in one period of time.


And do not forget about the size of the data that the application sends and receives. The time during which the transceiver will work depends on this, the smaller the size, the faster it will turn off. Therefore, we try to compress and cache everything.


The practice of reducing energy consumption in the background


Consider practicing in moments when the application is in the background. Activities for this section can be divided into four groups:


Notifications

Notifications are divided into Local (scheduled by the application) and Push (sent from the server). Local notifications do not require interaction over the network and do not launch the application. When notifying via APNS, the device will wake up and the application will be launched in the background to process the notification (if you need to upload data to display the notification).
How to reduce energy consumption here? Use local notifications whenever possible. Also set priority for APNS notifications and, depending on it, delivery will occur immediately, or the optimal time will be selected by the system.


VoIP

Previously, VoIP could only be supported until the application went into the terminated state. It was necessary to manually resume the session, poll the server, because of this, energy was consumed quickly. Now, increased consumption can be avoided: PUSH notifications for calls have appeared. Those. when such a notification arrives, the application wakes up and starts processing the call. And there is no longer need to maintain a permanent connection to the server. Use the new API to reduce power consumption.


Location

The process of determining geolocation causes a strong power consumption, due to the fact that you need to keep the GPS receiver active for a long time to position the device. When using GPS, optimization of power consumption leads to a decrease in the accuracy of geolocation determination. In applications such as Pokemon Go games, a decrease in the accuracy of geolocation is unacceptable, but in many other situations this is permissible.


There are many LocationManager settings that allow you to set the required accuracy in meters, trigger conditions, timeouts. Now we will briefly review the main techniques for reducing energy consumption:



In short - use the worst accuracy of geo-positioning, which allows you to solve your business problem.


Bluetooth

Bluetooth communication also requires the use of a transceiver, so to save power, we buffer data, reducing the number of transmission sessions, and also group sessions by time.


There is another possibility of saving - do not “wake up” the application by events from the bluetooth receiver.


For example, remember the fitness app. We optimized its work so that the signals from the LocationManager were processed only when the user opened the application. At this very moment, it is worthwhile to contact a fitness tracker via bluetooth and collect pulse data. This will not keep in touch with the fitness tracker all the time, collecting information about the pulse, because no one will look at it anyway while running.


Summary


Energy consumption increases rapidly, but decreases long after work. Use tricks to reduce power consumption:



')

Source: https://habr.com/ru/post/354312/


All Articles