iOS Grand Central Dispatch

  1. Wikipedia
    Grand Central Dispatch (GCD) is a technology developed by Apple Inc. to optimize application support for systems with multi-core processors and other symmetric multiprocessing systems.[2] It is an implementation of task parallelism based on the thread pool pattern. The fundamental idea is to move the management of the thread pool out of the hands of the developer, and closer to the operating system. The developer injects “work packages” into the pool oblivious of the pool’s architecture. This model improves simplicity, portability and performance.
  2. raywenderlich: Grand Central Dispatch In-Depth: Part 1/2Some highlights from the above mentioned link.

    What is GCD?
    GCD is the marketing name for libdispatch, Apple’s library that provides support for concurrent code execution on multicore hardware on iOS and OS X. It offers the following benefits:
    GCD can improve your app’s responsiveness by helping you defer computationally expensive tasks and run them in the background.
    GCD provides an easier concurrency model than locks and threads and helps to avoid concurrency bugs.
    GCD can potentially optimize your code with higher performance primitives for common patterns such as singletons.

    It’s often easy to overload UIViewController’s viewDidLoad with too much clutter; this often results in longer waits before the view controller appears. If possible, it’s best to offload some work to be done in the background if it’s not absolutely essential at load time.

    This sounds like a job for dispatch_async!

    - (void)viewDidLoad
        [super viewDidLoad];
        NSAssert(_image, @"Image not set; required to use view controller");
        self.photoImageView.image = _image;
        //Resize if neccessary to ensure it's not pixelated
        if (_image.size.height <= self.photoImageView.bounds.size.height &&
            _image.size.width <= self.photoImageView.bounds.size.width) {
            [self.photoImageView setContentMode:UIViewContentModeCenter];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1
            UIImage *overlayImage = [self faceOverlayImageFromImage:_image];
            dispatch_async(dispatch_get_main_queue(), ^{ // 2
                [self fadeInNewImage:overlayImage]; // 3

    dispatch_async: Asynchronous block
    dispatch_get_global_queue: A global queue off of the main thread
    dispatch_get_main_queue: A block of work to the main queue to access UI Kit classes

    The following is the summary of what more to expect from this reference tutorial.

    how and when to use the various queue types with dispatch_async:

    • Custom Serial Queue
    • Main Queue (Serial)
    • Concurrent Queue: This is a common choice to perform non-UI work in the background.

    Delaying Work with dispatch_after

    Wondering when it’s appropriate to use dispatch_after?

    • Custom Serial Queue: Use caution when using dispatch_after on a custom serial queue. You’re better off sticking to the main queue.
    • Main Queue (Serial): This is a good choice for dispatch_after; Xcode has a nice autocomplete template for this.
    • Concurrent Queue: Use caution when using dispatch_after on custom concurrent queues; it’s rare that you’ll do this. Stick to the main queue for these operations.

    Making Your Singletons Thread-Safe

    Singletons. Love them or hate them, they’re as popular in iOS as cats are on the web. :]

    dispatch_once() executes a block once and only once in a thread safe manner

    Handling the Readers and Writers Problem

    • GitHub Code Example
    •  Multithreading and Grand Central Dispatch on iOS for Beginners
      1. Summary
        Have you ever written an app where you tried to do something, and there was a long pause while the UI was unresponsive?This is usually a sign that your app needs multithreading!In this tutorial, you’ll get hands on experience with the core multithreading API available on iOS: Grand Central Dispatch.

        Downloading Asynchronously

        Let’s start by replacing the slowest operation with asynchronous calls – the downloading of the files.

        It’s actually not that difficult to do this with the built-in Apple classes – NSURLRequest and NSURLConnection – but I’m a fan of some wrapper classes that make this even easier – ASIHTTPRequest.

  3. raywenderlich: Grand Central Dispatch In-Depth: Part 2/2
  4. raywenderlich: Grand Central Dispatch Tutorial for Swift: Part 1/2
  5. raywenderlich: Grand Central Dispatch Tutorial for Swift: Part 2/2
  1. Concurrent Programming: APIs and Challenges and more detail about GCD in the article about low-level concurrency APIs.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s