IPhone: Grand Central Dispatch

Source: Internet
Author: User

There are three types of queue

    • Main:Tasks execute serially on your application's main thread

    • Concurrent:Tasks start executing in FIFO order, but can run concurrently.

    • Serial:Tasks execute one at a time in FIFO order

    • (1) Serial queues (Serial Queue), also known as private scheduling queue, is generally used for Synchronous access to specific resources. We can create any number of serial queues as needed. Each serial queue is concurrent.
    • (2) Parallel queue, also known as global dispatch queue. Although parallel queues can run multiple tasks concurrently, the execution sequence of the tasks starts is the same as that of the tasks added to the queue. We cannot create parallel scheduling queues by ourselves. There are only three available global concurrent queues.
    • (3) Main dispatch queue is a globally available serial queue.Program. Tasks in this queue alternate with the event source to be executed in the main loop of the application. Because it runs in the main thread of the application, main queue is often used as a synchronization point of the application.

 

 

Important functions in this c API
Creating and releasing queues
Dispatch_queue_t dispatch_queue_create (const char * label, null );// Serial queue
Void dispatch_release (dispatch_queue_t );

Putting blocks in the queue

TypedefVoid(^ Dispatch_block_t) (void );

VoidDispatch_async (dispatch_queue_t queue, dispatch_block_t block );

Getting the current or main queue

Dispatch_queue_tDispatch_get_current_queue ();
VoidDispatch_queue_retain (dispatch_queue_t );// Keep it in the heapDispatch_release

 

Main queue:Dispatch_queue_tDispatch_get_main_queue ();

Global queue: dispatch_get_global_queue (dispatch_queue_priority_default, 0 );

Serial: dispatch_queue_create ("identifier", null );

 

 

Example... assume we fetched an image from the Network (this wocould be slow ).

 
-(Void) viewwillappear :( bool) animated
 
{Dispatch_queue_t downloadqueue = dispatch_queue_create ("image downloader", null );
Dispatch_async (downloadqueue, ^ {nsdata * imagedata = [nsdata datawithcontentsofurl: networkurl];
// Uikit can only be dispatch_async (dispatch_get_main_queue (), ^ {
 
Uiimage * image = [uiimage imagewithdata: imagedata];
 
Self. imageview. Image = image;
 
Self. imageview. Frame = cgrectmake (0, 0, image. Size. Width, image. Size. Height );
 
Self. scrollview. contentsize = image. size ;});
 
}); Dispatch_release (downloadqueue );

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.