Concurrency

Grand Central Dispatch (GCD)

With GCD you add blocks of code to queues, and GCD manages the thread pool behind the scenes. In other words, GCD used threads under the hood.

Dispatch once

dispatch_once(&onceToken, ^{
    ...
});

Dispatch with delay

double delayInSeconds = 2.0;

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) (delayInSeconds * NSEC_PER_SEC));

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    ...
});

Queues

Queues can be serial (default) or concurrent.

Execute code on the main queue

Getting the Global Concurrent Dispatch Queues

Concurrent dispatch queues are useful when you have multiple tasks that can run in parallel.

Priority can be DISPATCH_QUEUE_PRIORITY_HIGH or DISPATCH_QUEUE_PRIORITY_LOW

Creating Serial Dispatch Queues

Serial queues are useful when you want your tasks to execute in a specific order. A serial queue executes only one task at a time and always pulls tasks from the head of the queue.

Queue Groups

Useful for chaining asynchronous blocks together to perform a given task.

Isolation Queues

Operation Queues

Creating a serial queue on the main thread

Adding a block to an operation queue

Timers

Creating a timer

Watching Files and Directories

Watching a directory

Last updated

Was this helpful?