grandCentral

How To Use Grand Central Dispatch – Xcode

Featured image by Paolo Villanueva from flickr. Modified by minor cropping. Used under creative commons attribution 2.0 generic.

 

 

Grand Central Dispatch is a very useful tool while developing iOS apps. It allows you to perform tasks in the background without interrupting the user’s experience. Watch the video above or follow the written tutorial below to learn how to use Grand Central Dispatch.

If you are not familiar with Xcode, please read my tutorial on Getting Started With Xcode.

Not interested in following the tutorial? Go ahead and download the completed project at the end of the tutorial!

Note:
This tutorial is using Xcode 6.
If you are experienced with Xcode, feel free to jump down to Step 5 and see how to use Grand Central Dispatch.

Step 0)Setting Up

First off, open Xcode and create a new project (File->New->Project).
Select “Single View Application” and then click “Next”.  (See Image 0-1)
Name your project, and then make sure the “Language” is “Objective-C”.
Click “Next”.

Image 0-1
Image 0-1

Image 0-2
Image 0-2
 

Step 1) Getting Started

Open your Main.storyboard file.
Add a UIButton and a UISlider so that your storyboard looks similar to Image 1-1.

Image 1-1
Image 1-1
Note: The use of auto-layout is beyond this simple tutorial. For an in-depth overview, read the Beginning Auto Layout Tutorial by Ray Wenderlich.

 

Step 2) Definitions

Open ViewController.h
Add the following IBAction before the @end. (See Image 2-1)

 

Image 2-1
Image 2-1

Step 3) Connecting

Now let’s connect our action and properties.
Open up the Main.storyboard.
Click on your view controller that has the button and slider.
Click on the yellow button found at the top of your view controller. (Image 3-1)
Click on the arrow in the circle on the right side of the screen (Image 3-2)
Now click and hold onto the circle next to sleepButton and drag it onto the sleep button.
Now click and hold onto the circle next to slider and drag it onto the slider.

Now click and hold onto the circle next to sleepAction: and drag it onto the sleep button. Select “Touch Up Inside”

Image 3-1
Image 3-1

Image 3-2
Image 3-2
 

Step 4) Prepare

Open ViewController.m
Add the following to define some basic functions.

 

Step 5) Grand Central Dispatch

Now for the moment you’ve been waiting for! Let’s go to sleep in the background!
Add the following action (In ViewController.m)

Now let’s go into detail on what the above code is doing.
1: This simply sets the title of the button previously added to Sleeping.
2: This creates a BOOL, which can either be NO or YES. You can use this to change whether you want to sleep in the background or not.
3: This checks if isPerformingInBackground is YES
4: This creates our variable my_queue_t and defines it default queue provided by the system. We will do intense work on this queue.
5: This simply opens up our queue, allowing us to start doing work on it. Anything done inside these {} will not interrupt the user experience.
6: This is where the “heavy work” is done. In our case, it the program just “sleeps”, or pauses.
7: This opens another default queue, this time it’s the main queue. This is where you need to do all UI updates.
8: This simply calls the method doneSleeping, which then updates the button title to “Sleep”.
9: This is called only  if isPerformingInBackground is NO.

Step 6) Run

Run your project by clicking on the triangle button at the top left of Xcode. If all goes well, the simulator will launch and you will be able to see the results of your hard work.
If it doesn’t work, compare your code with the code provided above. Also, check that you successfully connected your button and slider.
If all else fails, download the example project below.

Download Final Project

Feel free to download the final project below.

Download Now

 

Leave a Reply

Your email address will not be published. Required fields are marked *