Archive for August, 2012

Using GCD with Blocks

GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects.

Queues are a C typedef viz. dispatch_queue_t. GCD offers three kinds of queues, main, concurrent and serial.

Main queue is automatically created by the system and associated with your application. You can get it by using dispatch_get_main_queue().

Concurrent queue can be created using dispatch_get_global_queue and it executes the tasks, submitted in the form of block in FIFO order, however, the tasks run concurrently and can finish in any order.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);


Serial queues execute tasks in a strict FIFO serial order one at a time. To create serial queue use dispatch_queue_create which takes the name of the queue as first argument and options (currently NULL) as second argument.

dispatch_queue_create(“com.example.MyQueue”, NULL);

Submitting a block to Queue for Execution

You can submit a block to queue for execution using two of GCD provided functions:

dispatch_async OR dispatch_sync

Both the functions accept queue as the first argument and block as the second argument. dispatch_async returns immediately and executes the task asynchronously while dispatch_sync blocks execution until the provided block returns.

One good demonstration of using GCD API’s with concurrent and main queue and blocks is this blog entry from Jeff Kelly’s blog:

I also like Andy’s blog entry on testing or debugging block based API’s using the code snippet he created:

In fact, you can use Jeff’s idea of async loading of table cells along with Andy’s code snippet to test the async loading.






Leave a comment

Objective C – Some basics

If you are developing for the iOS platform, its invaluable to have foundational knowledge in Objective C. This brief blog posts covers some fundamental terms in objective C, hope you will find it useful.

1. Protocols – Protocols in objective C are equivalent to Interfaces in Java. They contain method definitions, either required or optional methods, which the implementing class implements. A good example is UITableViewDataSource Protocol. A class can implement multiple Protocols. For more details, refer to :

2. Categories – A category allows you to add methods to an existing class even if you do not have a the source code available for that class! Categories offers a neat alternative to subclassing by allowing you to extend the functionality of a class. For example, to add functionality to class named MyCategoryClass you can write the following code:

#import “MyCategoryClass.h”

@interface MyCategoryClass (CategoryName)

//method declarations



The category name indicates that the methods are additions to a class declared elsewhere, not a new class. Note that you cannot use a category to add additional instance variables to a class.

3. Properties – Properties provide a simple way to declare and implement an object’s accessor methods. There are two key parts to understanding properties in objective C, declaration and implementation.

You would normally declare a property in class header file.

Property Attributes —

Mutability – readwrite or readonly, default is readwrite

e.g. @property (readonly) int voltage;

Lifetime Specifiers — unsafe-unretained (default) e.g. @property int voltage (voltage in this case is unsafe unretained)

strong — ensures that a strong reference or ownership is kept to the passed-in object.

weak — does not imply ownership of the object pointed to. E.g. in case of parent child relationship, parent should have a strong reference to child, however, the child should have a weak reference. So that when child is deallocated, the parent reference to child can be released.

copy — in this case the setter will make a copy of the new object passed and set the pointer to point to the copied object.

e.g. @property (copy) NSString *serialNumber;

– (void) setSerialNumber: (NSString *) newSerialNumber {

newSerialNumber = [newSerialNumber copy];

[serialNumber release]; // releasing the original pointer

serialNumber = newSerialNumber;


4. Key Value Coding

KV is the ability to set and read property using its name. e.g. if we have a property name productName declared in a class whose instance is ‘a’, we can use the following code:

[a setValue@”Washing Machine” forKey@”productName”];





Leave a comment