Respond to Thermal State Changes
As more apps use more resources on a device, the system may begin enacting measures to stay cool and responsive. Strictly adhering to energy guidelines is the best way for app developers to proactively assist the system in this regard. However, apps can register to receive notifications when thermal state changes occur, such as when another app on the system is operating inefficiently and causes the temperature to rise. Apps can also call Foundation framework APIs in NSProcessInfo
to inquire about the thermal state of the device at any time.
Through thermal state awareness, apps with a high energy impact can take corrective actions to help the system return to an acceptable level if heat elevation does occur. As a result of a collaborative effort to keep heat low, users will experience cooler, quieter, and more responsive devices that also have great battery life.
Thermal States
There are four possible thermal states for a device, which are defined in NSProcessInfoThermalState
. See Table 18-1.
State/constant |
Impact |
Recommended action |
---|---|---|
Nominal /
|
The device's temperature-related conditions (thermals) are at an acceptable level. There is no noticeable negative impact to the user. |
No corrective action is necessary. As always, all apps should be proactive about optimizing energy use, in order to help ensure that thermals do not begin to rise. For example, discretionary background work should be assigned quality of service levels and scheduled for execution at optimal times using |
Fair /
|
Thermals are minimally elevated. On devices with fans, those fans may become active, audible, and distracting to the user. Energy usage is elevated, potentially reducing battery life. |
Although no corrective action is required, reaching this level provides an opportunity for greater proactive action. An app can begin reducing CPU usage and enacting other energy-saving measures to help ensure that the thermal state doesn’t rise any further. |
Serious /
|
Thermals are highly elevated. Fans are active, running at maximum speed, audible, and distracting to the user. System performance may also be impacted as the system begins enacting countermeasures to reduce thermals to a more acceptable level. |
Apps should begin to take corrective action to help the system reduce thermals and create a more optimal user experience. To achieve this, wherever possible, apps should:
|
Critical / |
Thermals are significantly elevated. The device needs to cool down. |
Apps can help the system by taking immediate corrective action to reduce usage of the CPU, GPU, and I/O to the absolute minimum level necessary in order to respond to user actions. Wherever possible, apps can stop using peripherals, such as the camera. |
Register for Thermal State Notifications
An app can register to receive notifications when the thermal state of a device changes. These notifications are posted on the global dispatch queue.
To register for thermal notifications, send the message addObserver:selector:name:object:
to the default notification center of your app (an instance of NSNotificationCenter
). Pass it a selector to call and NSProcessInfoThermalStateDidChangeNotification
, as shown in Listing 18-1.
Once an app receives notification of a thermal state change, it can query thermalState
in order to determine the current thermal state. See Listing 18-2. The app can then take proactive or corrective action, as appropriate.
Objective-C
[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(yourMethodName:)
name: NSProcessInfoThermalStateDidChangeNotification
object: nil];
Swift
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "yourMethodName:",
name: NSProcessInfoThermalStateDidChangeNotification,
object: nil
)
Determine the Thermal State
An app can query the current thermal state of a device at any time by accessing the thermalState
property of NSProcessInfo
, as shown in Listing 18-2. If the device’s thermal state is unknown, or if the device doesn’t support thermal awareness, then this property will always present thermals as nominal.
Objective-C
NSProcessInfoThermalState state = [[NSProcessInfo processInfo] thermalState];
if (state == NSProcessInfoThermalStateFair) {
// Thermals are fair. Consider taking proactive measures to prevent higher thermals.
} else if (state == NSProcessInfoThermalStateSerious) {
// Thermals are highly elevated. Help the system by taking corrective action.
} else if (state == NSProcessInfoThermalStateCritical) {
// Thermals are seriously elevated. Help the system by taking immediate corrective action.
} else {
// Thermals are okay. Go about your business.
};
Swift
let state = NSProcessInfo.processInfo().thermalState;
if state == NSProcessInfoThermalStateFair {
// Thermals are fair. Consider taking proactive measures to prevent higher thermals.
} else if state == NSProcessInfoThermalStateSerious {
// Thermals are highly elevated. Help the system by taking corrective action.
} else if state == NSProcessInfoThermalStateCritical {
// Thermals are seriously elevated. Help the system by taking immediate corrective action.
} else {
// Thermals are okay. Go about your business.
}
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13