Documentation Archive

Developer

Energy Efficiency Guide for Mac Apps

On This Page

Manage Tasks with CTS and GCD

The main thread of your app is where your app should handle user input, rather than long-running, processor-intensive, and discretionary operations. Always move those tasks onto background threads. Moving tasks into the background leaves your main thread free to continue processing user input. This is especially important when your app is starting up or quitting, because it is expected to respond to events in a timely manner. Centralized Task Scheduling (CTS) and Grand Central Dispatch (GCD) APIs help you schedule and manage background activity.

Centralized Task Scheduling (CTS)

Centralized Task Scheduling APIs allow you to designate criteria for when a task should be performed, such as when a user plugs the computer into power or when the system is not performing higher-priority tasks. The system can intelligently decide when to perform the task based on the specified criteria.

The following sections discuss how to defer execution of discretionary tasks by using CTS:

Grand Central Dispatch (GCD)

Grand Central Dispatch is a low-level framework in OS X that manages concurrent and asynchronous execution of tasks across the operating system. Essentially, tasks are queued and scheduled for execution as processor cores become available. By allowing the system to control the allocation of threads to tasks, GCD uses resources more effectively, which help the system and apps run faster, efficiently, and responsively.

GCD supports the implementation of dispatch queues, which execute arbitrary blocks of code asynchronously or synchronously. Use dispatch queues to perform nearly all of the tasks that could be performed on separate threads. Dispatch queues are easier and more efficient to use than the corresponding threaded code. Serial dispatch queues are a good alternative to using timers for synchronization.

For detailed information about implementing GCD features in your app, refer to Concurrency Programming Guide and Grand Central Dispatch (GCD) Reference.