The basic use of the iOS development multithreaded article-GCD

Source: Internet
Author: User
Tags 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:

1//2//  YYVIEWCONTROLLER.M 3//  12-GCD Basic use (home row) 4//5//  Created by Hole Doctor on 14-6-25.6//  Copyright (c) 2014 itcast. All rights reserved. 7//8  9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 {     [super viewdidload];20]/     /Print main thread      (@ "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?

1//2//  YYVIEWCONTROLLER.M 3//  13-GCD Basic Use (problem) 4//5//  Created by Hole Medical has on 14-6-25.6//  Copyright (c) 2 014 Years itcast. All rights reserved. 7//8  9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(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]);/     /sync function     dispatch_sync (queue, ^{36         NSLog (@ "Task 2 thread----%@" , [Nsthread CurrentThread]);     }39 @end

Printing results:

2. Turn on the sub-thread and load the picture

 1//2//YYVIEWCONTROLLER.M 3//14-GCD basic use (download image) 4//5//Created by Hole Medical has on 14-6-25. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimagev     Iew *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 CURRENTTH Read]); 35//3. Download pictures from the web nsurl *urlstr=[nsurl urlwithstring:@ "http://h.hiphotos.baidu.com/baike/w%3D268/sign =30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg "];37 NSData *data=[NSData DataWithcontentsofurl:urlstr];38 UIImage *image=[uiimage imagewithdata:data];39//Hint NSLog (@ "Picture loading complete"); 41 42//4. Back to the main thread, show pictures [Self.imageview performselectoronmainthread: @selector (setimage:) withobject:i Mage 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.

 1//2//YYVIEWCONTROLLER.M 3//14-GCD basic use (download image) 4//5//Created by Hole Medical has on 14-6-25. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimagev     Iew *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 CURRENTTH Read]); 35//3. Download pictures from the web nsurl *urlstr=[nsurl urlwithstring:@ "http://h.hiphotos.baidu.com/baike/w%3D268/sign =30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg "];37 NSData *data=[NSData DataWithcontentsofurl:urlstr];38 UIImage *image=[uiimage imagewithdata:data];39//Hint NSLog (@ "Picture loading complete"); 41 42//4. Back to the main thread, show picture of//[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 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});

The basic use of the iOS development multithreaded article-GCD

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.