Basic use of GCD
First, the home row introduction
Main queue: is the queue associated with the main line threads, the home row is a special serial queue of GCD, the task placed in the main queue, will be placed in the main thread to execute. Tip: If you put the task in the main queue for processing, the new thread will not open regardless of whether the handler is asynchronous or synchronous. How to get the home column:
dispatch_queue_t Queue=dispatch_get_main_queue ();
(1) using an asynchronous function to perform a task in the main queue, code example:
8 9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation YYViewController1 6-(void) viewDidLoad18 { [super viewdidload];20]/ /Print main thread: NSLog (@ "Print main thread--%@", [Nsthread Mainthread]); //1. Get home row dispatch_queue_t queue=dispatch_get_main_queue (); //2. Add the task to the main queue to execute the dispatch_async (queue, ^{28 NSLog (@ "Use asynchronous functions to perform task 1--%@ in the main queue", [Nsthread CurrentThread]); 29 }); dispatch_async (queue, ^{31 NSLog (@ "Use asynchronous functions to perform task 2--%@ in the main queue", [Nsthread CurrentThread]); 33 dispatch_async (queue, ^{34 NSLog (@ "Use asynchronous functions to perform tasks in the main queue 3--%@", [Nsthread CurrentThread]); 36}37 38 @end
Execution effect:
(2) using the synchronization function, in the main thread to perform the task in the master queue, there will be a dead loop, the task can not be executed down. As follows:
Second, basic use
1. Questions
Do Task 1 and Task 2 execute on the main thread or a child thread, or do you open a new thread separately?
8 9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation YYViewController1 6-(void) viewDidLoad18 { [super viewdidload];20 ///Open a background thread, call the Execute test method [self] Performselectorinbackground: @selector (Test) withobject:nil];22}23-(void) Test25 {NSLog (@ "Current thread---%@", [ Nsthread CurrentThread]); dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_default , 0); Dispatch_async//Async function (queue, ^{31 NSLog (@ "Task 1 thread----%@", [Nsthread CurrentThread]) ), dispatch_sync//Sync function (queue, ^{36 NSLog (@ "Task 2 thread----%@", [Nsthread CurrentThread]); }39 @end
Printing results:
2. Turn on the sub-thread and load the picture
8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageview;13 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21 22}23 24//When the finger touches the screen, download a picture from the network to the Controller view on the display (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event26 {27 28//1. Get a global serial queue dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); 30 2. Add the task to the queue execute Dispatch_async (queue, ^{32 33//Print current thread NSLog (@ "%@", [Nsthread CurrentThread ]); 35//3. Download pictures from the web nsurl *urlstr=[nsurl urlwithstring:@ "http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b 3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg "];37 NSData *data=[NSData DataWithCont entsofurl:urlstr];38 UIImage *image=[uiimage imagewithdata:data];39//Hint NSLog (@ "Picture loading complete"); 41 42//4. Back to the main thread, show picture [Self.imageview performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no];44 });}46 @end
Display effect:
Printing results:
Requires the use of GCD, after the loads download the picture, the main thread gets the loaded image to refresh the UI interface.
8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageview;13 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21 22}23 24//When the finger touches the screen, download a picture from the network to the Controller view on the display (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event26 {27 28//1. Get a global serial queue dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); 30 2. Add the task to the queue execute Dispatch_async (queue, ^{32 33//Print current thread NSLog (@ "%@", [Nsthread CurrentThread ]); 35//3. Download pictures from the web nsurl *urlstr=[nsurl urlwithstring:@ "http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b 3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg "];37 NSData *data=[NSData DataWithCont entsofurl:urlstr];38 UIImage *image=[uiimage imagewithdata:data];39//Hint NSLog (@ "Picture loading complete"); 41 42//4. Back to the main thread, photo gallery//[Self.imageview Performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no];44 Dispatch_async (Dispatch_get_main_queue (), ^{45 self.imageview.image=image;46//print Current thread 47 NSLog (@ "%@", [Nsthread CurrentThread]););}51 @end
Printing results:
Benefits: All data in a sub-thread can be used directly in the main thread, which is more convenient and intuitive.
Third, inter-thread communication
Returning from a child thread to the main thread
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{//time-consuming asynchronous operation ... Dispatch_async ( Dispatch_get_main_queue (), ^{//back to the main thread, hold? UI refresh Operation});
Basic use of GCD