Sie sind auf Seite 1von 5

GCD and Dispatch Queues in

iPhone Application

GCD and Dispatch Queues in


iPhone Application
Grand Central Dispatch (GCD) is a technology available on iOS, which provides
efficient concurrent code execution support. GCD is a lightweight multi-threading
engine. GCD helps to keep the UI responsive.
Developers many times face requirements where the iPhone application being
developed requires downloading huge files, and in such a case, the general
behavior is the application shows an activity indicator like Loading
Meanwhile, user cannot perform any task until the download is completed. The
reason is, you are downloading your file synchronously on main thread.
If your code already has synchronous calls on the main thread, and you are looking
for a way to make them asynchronous with minimum effort, then GCD is an easy
way to achieve transition.
GCD will automatically manage the main and background thread so you dont
have to worry about it.
Below is an example to download zip file and process it. This will increase work
on main thread and your UI is also responds on the main thread so surely there is a
heavy load on the main thread.
[request setCompletionBlock:^{
NSLog(@"Zip file downloaded.");
NSData *data = [request responseData];

[self processZip:data sourceURL:sourceURL]; // Heavy work on main


thread
}];
Now question arises that how to run this heavy work in the background. The very
simple but very efficient way to solve this problem is via GCD. Whenever you
want to run something in the background, then you just call dispatch_async and
pass in some code to run.
GCD will manage all the activities for you, viz. it will create a new thread for
youre given task if it needs to or reuse an old one if it is available.
In dispatch_async call you pass in a dispatch queue. Think of this queue as a list
that stores all the blocks that you pass in and works in FIFO.
If you think that you should create a new queue for your task then you can create it
using dispatch_create. If you want to access main thread then you can get it using
dispatch_get_main_queue. For our example we will make a background queue
called backgroundQueue. We will use this queue to run our task in background
like parsing XML or downloading and unzipping any zip files etc.
By default behavior of any dispatch queue is serial which means only one block of
code from the queue, is executed at a time. This is a good behavior when you want
to protect shared data usage.
You may restrict other queue to use of particular data structure and only accessed
by code running within a particular dispatch queue.
Because of its serial behavior you are guaranteed that only one block of code will
access the data structure at a time.

Grand Central Dispatch in Practice


Open header file in which you want to implement GCD
// Add to top of file
#import <dispatch/dispatch.h>
// Add new instance variable
dispatch_queue_t backgroundQueue;
To use Grand Central Dispatch, you first need to import.
We have also declared dispatch queue that we will use to run our background
processing tasks.
Next open up .m file and make the following changes:
// 1) Create background Queue
backgroundQueue = dispatch_queue_create(com.specindia.backgroundQueue, NULL);
// 2) Add to top of dealloc
dispatch_release(backgroundQueue);
// 3) Modify process to be the following
- (void)process {
dispatch_async(backgroundQueue, ^(void) {
// Call method/block of code which downloads zip file in background

});
}
// 4) Modify call method inside other method to be the following
dispatch_async(backgroundQueue, ^(void) {
// Method which process zip files and give you original data
});
// 5) Modify call to delegate at the end of download zip AND process zip to be
the following
dispatch_async(dispatch_get_main_queue(), ^(void) {
// Call method to change UI as per background task
});
These all are simple but important steps,
1) While creating dispatch queue, ensure that each dispatch queue has its
unique name. So when you are creating it provide unique name to it. The
best way to do this is to use reverse DNS notation.
2) Dont forget to release dispatch queue in dealloc method.
3) Run download file process in the backgroundQueue in the background
thread, with a simple call to dispatch_async!
4) Similarly, after downloading file, call another method to unzip downloaded
file in background thread. Reusing same dispatch queue does it.
5) Finally switch Main thread to show update user interface.

Das könnte Ihnen auch gefallen