Click here to Skip to main content
13,357,363 members (67,493 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 26 May 2011

iOS 4 in Action - Fetching Events with Grand Central Dispatch (GCD)

, 26 May 2011
Rate this:
Please Sign up or sign in to vote.
A chapter excerpt from iOS 4 in Action- Fetching events with Grand Central Dispatch (GCD)
image002.jpgiOS 4 in Action
Developing iPhone & iPad Apps

By Jocelyn Harrington, Brandon Trebitowski, Christopher Allen, and Shannon Appelcline

Though multicore is not available on iOS devices, Grand Central Dispatch helps the application to run faster, more efficiently, and asynchronously. In this article, based on chapter 16 of iOS 4 in Action, the authors show you how GCD is used to fetch events.

To save 35% on your next purchase use Promotional Code jharrington1635 when you check out at

You may also be interested in…

Grand Central Dispatch (GCD) was available on Mac OS as comprehensive improvements to support concurrent code execution on multicore hardware technology. Now, it’s available on iOS 4 as a C API. Though multicore is not available on iOS devices, GCD helps the application to run faster, more efficiently, and asynchronously. You can find more reference on Concurrency Programming Guide from the iOS developer website. Let’s look at GCD and use it to fetch events.

How does it work?

GCD is available to any application and there is no need to add in any other framework. You can imagine that you have queues of operations and each queue is running in its own thread separately by the system. You don’t have to worry about when to run the time-consuming operations. The system will take care of the order and make sure that the queue’s operations get done.

In order to keep the application running responsively, the key concept is not to block the main thread. Throw a time-consuming task, such as fetching a list of events from the Calendar’s database or downloading an image file from the Internet, to the background thread. Then, update the UI after the work in the background is done, and the data is ready to display.

How do you accomplish this task with GCD? It turns out, it’s pretty easy. Simply, call the method dispatch_async() to submit a queue of operations (a block of code) to the main queue and execute the task on a dispatch queue asynchronously, and then tell the system to update the UI.

The definition for dispatch_async() is listed as:

void dispatch_async( dispatch_queue_t queue,
                  dispatch_block_t block

How do we create a queue or get the main queue? Here are some common methods related to GCD:

  • Creating a queue:
    dispatch_queue_create(const char *label, NULL); 
  • Releasing a queue:
  • Getting the main queue:

Please don’t forget to release the queue when it is created with method dispatch_queue_create().

With GCD and blocks in hand, let’s see how to create a queue for the fetching events operations and let the system decide when to run this queue, and then update the UI in the main thread.

Fetching Events with GCD

Let’s use the GCD and blocks to fetch the whole month of events and update the table view display.

Listing 1 Fetching events with GCD

-(void)fetchEventsForNextMonth {
    NSDate *startDate = [NSDate date];
    NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*30];
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:eventStore.calendars]; 
    dispatch_queue_t fetching_queue= 
dispatch_queue_create("Fetching events", NULL);#1
    dispatch_async(fetching_queue, ^{                      #2
           NSArray *eventList = 
[self.eventStore eventsMatchingPredicate:predicate];
           dispatch_async(dispatch_get_main_queue(), ^{    #3
                  [ addObjectsFromArray:eventList];
                  [self.tableView reloadData];
dispatch_release(fetching_queue); #4
#1 Defines a custom queue
#2 Fetches events 
#3 Updates UI
#4 Releases queue

In listing 1, we first create a custom queue (#1). The GCD function starts (#2). The block of operations will first execute the custom task to fetch events from the Calendar’s database on the dispatch queue. When the eventList array is ready, we want to update the table view’s UI with the new data on the main queue dispatch. The block (#3) first updates the events array and then reloads the table view’s data. Finally, don’t forget to release the queue (#4). Simply follow the design flow; GCD will automatically finish the task and provide a quick response asynchronously.

As you can see from the example, with a few lines of code, you managed to perform a task in the background and update the UI when the new data is ready. That’s the power of GCD and blocks.


The Event Kit framework provides an interface for accessing calendar events on a user’s device. You can use this framework to get existing events and add new events to the user’s calendar. With GCD, the application’s performance is improved significantly, especially when fetching events from Calendar’s database asynchronously.

Here are some other Manning titles you might be interested in:


Objective-C for the iPhone
Christopher K. Fairbairn and Collin Ruffenach


iPhone in Practice
Bear P. Cahill


Unlocking Android, Second Edition
W. Frank Ableson and Robi Sen


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralErrata on multicore iOS devices Pin
Tomé Vardasca26-May-11 8:44
memberTomé Vardasca26-May-11 8:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 26 May 2011
Article Copyright 2011 by Manning
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid