A great user experience requires:
好的用户体验需要:
iOS integrates with advanced hardware features such as a power efficient CPU, accelerated graphics, and wireless antennas. Hardware and software work together to deliver an optimized user experience that’s great for battery life.
iOS 设备集成了优秀的软件和硬件
iOS apps have a life cycle that’s managed by the system. When a user finishes interacting with an app, the app is placed into a background state, where activity is throttled and the app may be suspended. Apps generating high CPU usage for extended periods of time while running in the background may be terminated by the system, if necessary.
iOS 应用的生命周期是被系统管理的。系统会限制应用在后台的活动。
APIs let you designate criteria that indicate when and how often a network operation should be deferred, how long it can be deferred, and under what circumstances. The system uses this information to defer the operation until an energy efficient time.
支持通过 API 控制延迟网络请求。
Tasks that affect the user, such as downloading and playing music, take priority over background and discretionary work. Quality of service class APIs allow you to assign priority levels to the work your app performs, giving you fine-grained control over task prioritization.
影响用户的任务,优先级高于后台任务。也有 API 可以精细控制任务优先级。
Xcode and Instruments help you identify and address energy problems as you develop your app, rather than after those problems are encountered by users.
Xcode 和 Instruments 有助于在开发阶段就发现和解决能源问题。
Even small inefficiencies in apps add up, significantly affecting battery life, performance, and responsiveness. As an app developer, you have an obligation to make sure your app runs as efficiently as possible. Use recommended APIs so the system can make smart decisions about how best to manage your app and the resources it uses. Whenever possible, batch and reduce network operations, and avoid unnecessary updates to the user interface. Power-intensive operations should be under the user’s control. If a user is playing a graphics-heavy game, for example, the user should not be surprised if the activity consumes power. Strive to make your app absolutely idle when it is not responding to user input.
作为应用程序开发人员,您有义务确保您的应用程序尽可能高效地运行。
使用推荐的 API,以便系统能够明智地决定如何最好地管理应用程序及其使用的资源。只要有可能,批处理和减少网络操作,并避免对用户界面进行不必要的更新。功率密集型操作应由用户控制。努力让你的应用程序在没有响应用户输入时完全进入空闲状态。
There’s no single solution for conserving energy on a device. Numerous technologies and operations influence how energy is used:
在设备上没有单一的节能解决方案。许多技术和操作影响能源的使用:
Energy and power are two separate but related concepts. Power is an instantaneous measurement (watts) of energy required at any given point in time (Figure 2-1). Energy is a measurement of power used (joules) over a period of time (watt hours).
能量与功率是两个独立但相关的概念。功率是对任何给定时间点所需能量的瞬时测量(瓦特)。能量是对一段时间内(瓦时)所用功率的测量(焦耳)。
Figure 2-1 Energy is power over time
As more CPU is used, more power draw occurs, more energy is used, and the device’s battery drains faster. Power draw varies based on the device, processor, resources, and so on.
越多的 CPU 被使用,越多的功率被消耗,越多的能量被使用,设备的电池消耗越快。功率消耗因设备、处理器、资源等而异。
Table 2-1 Example of idle vs. CPU power draw
Idle | 10x greater power draw over sleep |
---|---|
1% CPU use | 10% greater power draw over idle |
10% CPU use | 2x power draw over idle |
100% CPU use | 10x power draw over idle |
Tasks your app performs have a dynamic cost—how much energy your app uses by doing actual work. They also have a fixed cost—how much energy is used by bringing the system and various resources up in order for your app to do work, and back down after that work is complete. When lots of sporadic work is occurring, there are dynamic costs and a significant fixed cost too, as resources may never get the chance to reach true idle between the sporadic tasks. This situation results in a lot of energy being used for a relatively small amount of actual work. See Figure 2-2.
应用程序执行的任务有动态开销——使用多少取决于实际的工作。同时也有固定开销——通过提升系统和各种资源以完成工作,并在工作完成后回落。
当大量零星工作发生时,会产生动态开销和大量的固定开销,因为在零星任务之间资源可能永远无法达到真正的空闲状态。这就导致了大量的能量被用于相对较少的实际工作。见图 2-2。
Figure 2-2 Fixed vs. dynamic energy costs
Your app can avoid sporadic work by batching tasks and performing them less frequently. For example, instead of performing a series of sequential tasks on the same thread, distribute those same tasks simultaneously across multiple threads, as shown in Figure 2-3. By batching activity, components can be powered up once and used over a shorter period of time.
可以通过批处理任务或降低它们的执行频率来避免零星工作。例如,不要在同一个线程上执行一系列连续的任务,而是将这些相同的任务同时分布在多个线程上,如图2-3所示。通过批处理活动,组件可以通电一次并在较短的时间内完成使用。
This strategy incurs a greater up-front dynamic cost. In exchange, you get a dramatic reduction in fixed cost, which results in tremendous energy savings over time.
这种策略需要更大的前期动态开销。作为交换,您可以大幅降低固定成本,这会随着时间的推移节省大量的能源。
As you develop your app, think holistically about its behavior, and try to reduce fixed costs wherever possible.
在开发应用程序时,要全面考虑其行为,并尽可能降低固定成本。
Figure 2-3 Use multithreading to trade power for energy