

Use GCD Tools for Synchronization Instead of Timers Monitoring Events in Cocoa Event Handling Guide Use the network reachability and connection APIs.ĭetermining Reachability and Getting Connected in System Configuration Programming GuidelinesĮvents generated by the mouse, keyboard, and other input devices Local and Remote Notification Programming GuideĭNS Service Discovery Programming Guide and NSNetServices and CFNetServices Programming Guide The availability of hot-pluggable devicesįinding and Accessing Devices in Accessing Hardware From Applications

Register for disk arbitration notifications. Use the notify_register, notify_post, and other notification functions. NSDistributedNotificationCenter Class Reference

Use the NSDistributedNotificationCenter class. Updates to systemwide files or directoriesĬreate an event stream with the File System Events API.Ĭreating XPC Services in Daemons and Services Programming Guide Table 7-1Obtaining event notifications for system servicesĪpproach to follow for obtaining event notificationsĭispatch Sources in Concurrency Programming Guide Listing 7-1Recommended: Use an energy-efficient dispatch source to obtain file change notifications Instead of using timers to watch for events, use a more efficient service, such as a dispatch source. Timers prevent the CPU from going to or staying in the idle state, which increases energy usage and consumes battery power.

Some apps use timers to monitor for key presses, mouse locations, changes to file contents, network availability, and other state changes. Regardless of the scenario, if there are many timer-invoked wakeups, the energy impact is high, as shown in Figure 7-1.įigure 7-1The Energy Impact gauge in Xcode showing the impact of excessive wakeups from idle Get Event Notifications Without Using Timers Some timers are executed without suitable timeouts, causing them to continue firing when they’re no longer needed. Other apps use timers as synchronization tools when they should use semaphores or other locks to achieve the greatest efficiency. For example, some apps use timers to poll for state changes when they should respond to events instead. If you use timers in your app, consider whether you truly need them. If a timer causes the system to wake, it incurs that cost.Īpps often use timers unnecessarily. Waking the system from an idle state incurs an energy cost when the CPU and other systems are awakened from their low-power, idle states. A timer waits until a certain interval has elapsed and then fires, performing a specific action such as sending a message to its target object. If you must use timers, employ them efficiently.Ī timer lets you schedule a delayed or periodic action. The XPC Activity API can be used to request centralized scheduling of discretionary tasks in your app. The NSURLSession API provides the ability to perform out-of-process background URL sessions and receive notifications when they are complete. The NSBackgroundActivityScheduler API lets you schedule both repeating and non-repeating tasks energy efficiently. You can reduce your app’s energy usage by implementing energy-efficient APIs instead of timers.
