In iOS, there are three ways to use multithreading, respectively: Nsthread, Nsoperation and Nsoperationqueue, GCD, in this section, mainly to explain the use of CDD.
GCD (Grand Central Dispatch), he is based on C language development of a set of multi-threaded development mechanism, is currently the official recommendation of Apple multithreading development method. GCD has the highest level of abstraction and is relatively simple to use, but because it is based on the C language, it is process oriented, so it is not as good as object-oriented understanding when it is used. But the most significant advantage of the GCD mechanism compared to the previous two multithreaded development methods is that it is more efficient for multicore operations.
GCD queue
GCD also has a queue similar to Nsoperationqueue, GCD Unified management of the tasks in the entire queue, the queue in GCD is divided into three kinds:
(1) Serial queue dispatch_queue_create (,): Only one thread, the actions added to the queue are executed sequentially in order of addition.
(2) Global Queue Dispatch_get_global_queue (,): There are multiple threads, and it will schedule these queues on the available processors, while ensuring that advanced tasks are prioritized.
(3) Home row dispatch_get_main_queue (): Used to perform operation tasks on the main thread.
GCD use
Loading pictures from the network uses a multi-threaded approach, where you can get pictures from the Web using the GCD method.
When using GCD to fetch data, there are synchronous Dispatch_sync (,) and asynchronous Dispatch_async (,), which generally choose to request data asynchronously.
In addition, if it is a set of pictures, you can choose to use serial queue request display, that is, in order to display, you can also use the global queue request display, the display is asynchronous concurrency, not in order random display. In the following code, there are two ways to describe each of them.
Code
//VIEWCONTROLLER.M//Gcd_demo////Created by Jerei on 15-11-13.//Copyright (c) 2015 jerehedu. All rights reserved.//#Import"ViewController.h"#define Img_view_width90#define Img_view_height45#define GAP ([UIScreen mainscreen].bounds.size.width-(2*img_view_width))/3.0)@InterfaceViewcontroller () @end @implementation Viewcontroller- (void) Viewdidload {[SuperViewdidload]; //ImageView to display pictures on the interface[self addimageviews]; //Add button[self addbtns];} #pragma Mark-ImageView to display pictures on the interface-(void) addimageviews{ for(inti=0; i<10; i++) {Uiimageview*imgview = [[Uiimageview alloc] Initwithframe:cgrectmake (gap+ (i%2) * (IMG_VIEW_WIDTH+GAP), + GAP + (I/2) * (img_view_ Height+20), Img_view_width, Img_view_height)]; Imgview.tag= I+1; Imgview.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:imgview]; }} #pragma mark-Button-(void) addbtns{UIButton*SERIAL_BTN = [[UIButton alloc] Initwithframe:cgrectmake (GAP, 30, Img_view_width, Img_view_height)]; [Serial_btn settitle:@"Serial Load"Forstate:uicontrolstatenormal]; [Serial_btn Setbackgroundcolor:[uicolor Purplecolor]; [Self.view ADDSUBVIEW:SERIAL_BTN]; [Serial_btn addtarget:self Action: @selector (loadimage_gcd_serial) forcontrolevents:uicontroleventtouchupinside]; UIButton*GLOBAL_BTN = [[UIButton alloc] Initwithframe:cgrectmake (2*gap+img_view_width, 30, Img_view_width, Img_view_height)]; [Global_btn settitle:@"Parallel Loading"Forstate:uicontrolstatenormal]; [Global_btn Setbackgroundcolor:[uicolor Purplecolor]; [Self.view ADDSUBVIEW:GLOBAL_BTN]; [Global_btn addtarget:self Action: @selector (Loadimage_gcd_global) forcontrolevents:uicontroleventtouchupinside];} #pragma Mark-load photos from the network serial-(void) loadimage_gcd_serial{//Serial Queuedispatch_queue_t serialqueue = dispatch_queue_create ("myThreadQueue1", dispatch_queue_serial); //Request Picture for(inti=0; i<10; i++) {Dispatch_async (Serialqueue,^{ //get pictures from the webNsurl *url = [Nsurl urlwithstring:@ "Http://www.jerehedu.com/images/temp/logo.gif"]; NSData*data =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:data]; //back to the main thread refresh interfaceDispatch_sync (Dispatch_get_main_queue (), ^{Uiimageview*currentimgview = (Uiimageview *) [Self.view viewwithtag:i+1]; Currentimgview.image=image; }); }); }} #pragma mark-load photos in parallel from the network-(void) loadimage_gcd_global{//Parallel Queuesdispatch_queue_t globalqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0); //Request Picture for(inti=0; i<10; i++) {Dispatch_async (Globalqueue,^{ //get pictures from the webNsurl *url = [Nsurl urlwithstring:@ "Http://www.jerehedu.com/images/temp/logo.gif"]; NSData*data =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:data]; //back to the main thread refresh interfaceDispatch_sync (Dispatch_get_main_queue (), ^{Uiimageview*currentimgview = (Uiimageview *) [Self.view viewwithtag:i+1]; Currentimgview.image=image; }); }); }}- (void) didreceivememorywarning {[Superdidreceivememorywarning];} @end
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSectionTechnology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:
Multi-thread GCD in iOS