Absolute Ripple
  • Absolute Ripple
  • Contact

Grand Central Dispatch

23/7/2013

0 Comments

 
Grand Central Dispatch (GCD) is a powerful multitasking technology in iOS. I don't pretend to know all the ins and outs about GCD, and haven't really use it much. Mostly, I tend to use it for just one thing: to prevent the main thread from blocking due to time consuming activities, such as downloading things from the network or serious image processing.
The code typically looks something like:
dispatch_queue_t nameOfQueue = dispatch_queue_create("SOME_NAME", NULL);
    dispatch_async(nameOfQueue, ^{
        /* codes to be performed in the other queue */

        /* eg. downloading something  */
        dispatch_async(dispatch_get_main_queue(), ^{
            /* codes to be performed in main thread, usually UIKit stuff */
            /* e.g. update the UI based on the data you have downloaded */
        });
    });


Note:
- the NULL in dispatch_queue_create means this is a serial queue, as opposed to a concurrent queue
- dispatch_get_main_queue() is used to get the main queue
- dispatch_get_current_queue() is used to get the current queue

It is important to dispatch back to the main queue if you want to run UIKit stuff as they are mostly not thread safe. This is what the second dispatch_async() is doing above. A few exceptions are: UIImage, UIFont, UIBezierPath, drawing things using Core Graphics. (Not sure if there are others.)

See the Apple Documentation.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    August 2013
    July 2013
    June 2013
    May 2013
    January 2013
    October 2012
    September 2012
    March 2012
    February 2012

    Categories

    All
    Apple Policy
    In App Purchase
    Ios Programming
    Our Apps

    RSS Feed

    Disclaimers

    Here are some resources that I find useful in writing iOS apps. These are not necessarily unique as they are based on various sources online.
    This is mainly to provide a source of reference to myself and my associates but if it can be of use to someone else, that is good.
    Note that information is provided here as is. Use at your own risk. There is no guarantee that it is accurate. We will update the information as time and resources permit.

    Powered by Create your own unique website with customizable templates.