GCD Dispatch Queue

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

IOS in multi-threaded programming tool mainly:

These three methods are simple and easy to use, Each has its own merits.. But there is no doubt that GCD is the most attractive, because it is apple for multi-core parallel computing solution. Although the current mobile platform with dual core is not much, but does not affect the GCD as the tool of multithreaded programming (ipad2 is a dual core, this is a trend).

GCD is closely connected with block, so it is best to first understand the block (see here).GCD is the function C level, which means that it also provides C function pointer as parameter, is convenient for the C programmer.

The first use of GCD:

 

dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

 

Async showed that the asynchronous operation, block is for you to do, queue is your task to handle. (in addition to async, there are sync, delay, taking async as an example).

The program will be used in multi thread because the procedure will often need to read data, and then update the UI. to a good user experience, data read operation will tend to run in the background, such as to avoid blocking the main thread of.GCD is three queue.

1. Main queue:

  As the name implies, run in the main thread, acquired by the dispatch_get_main_queue. And UI related to the use of Main Queue.

2.Serial quque(private dispatch queue)

  Each time you run a task, you can add more than one, the order of execution of FIFO. usually refers to the programmer generated, for example:

NSDate *da = [NSDate date];
NSString *daStr = [da description];
const char *queueName = [daStr UTF8String];
dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);

3. Concurrent queue(global dispatch queue):

You can run multiple tasks at the same time, each task start time is in accordance with the entry of queue sequence, the end of the sequence depending on task. Obtained using dispatch_get_global_queue.

So we can generally understand the use of the GCD framework:

 

Copy code
dispatch_async(getDataQueue,^{
    //To obtain the data, to obtain a group, refresh UI.
    dispatch_aysnc (mainQueue, ^{
    //The UI update to be carried out in the main thread
};
}
)
Copy code

 

Thus, GCD is very easy to use, in my experience, they will gradually phase out the use of NSOperation instead of GCD.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Nancy at December 16, 2013 - 4:32 PM