Multithreading GCD in IOS and IOS multithreading GCD

Source: Internet
Author: User

Multithreading GCD in IOS and IOS multithreading GCD

In ios, multithreading can be used in three ways: NSThread, NSOperation, NSOperationQueue, and GCD. In this section, we will mainly explain how to use CDD.

GCD (Grand Central Dispatch) is a multi-threaded development mechanism developed based on C language. It is also a multithreading development method officially recommended by Apple. GCD has the highest abstraction level and is easy to use. However, it is developed based on C language and process-oriented. Therefore, it is not as easy to understand as object-oriented. However, compared with the previous two multi-threaded development methods, GCD is more effective for multi-core computing.

GCD queue

GCD also has a queue similar to NSOperationQueue. GCD manages tasks in the entire queue in a unified manner. There are three queues in GCD:

(1) Serial queue dispatch_queue_create (,): There is only one thread, and the operations added to the queue are executed in order of adding.

(2) Global queue dispatch_get_global_queue (,): there are multiple threads. After the operation comes in, it will arrange these queues on available processors and ensure that advanced tasks are prioritized.

(3) dispatch_get_main_queue (): used to execute Operation tasks on the main thread.

GCD usage

The multi-thread method is used to load images from the network. Here, the GCD method is used to obtain images from the network.

When using GCD to retrieve data, two methods are available: Synchronous dispatch_sync (,) and asynchronous dispatch_async (,). Generally, data is requested asynchronously.

In addition, for a group of images, you can select serial queue request display, that is, display in order; you can also use global queue request display to display asynchronous concurrency, it is not displayed randomly in order. The following code describes two methods.

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_WIDTH 90 # define IMG_VIEW_HEIGHT 45 # define GAP ([UIScreen mainScreen]. bounds. size. width-(2 * IMG_VIEW_WIDTH)/3.0) @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // set the imageView [self addImageViews] of the image displayed on the interface; // Add the button [self addBtns];} # pragma mark-set the imageView (void) of the image displayed on the Interface) addImageViews {for (int I = 0; I <10; I ++) {UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (GAP + (I % 2) * (IMG_VIEW_WIDTH + GAP), 60 + 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 * parameters = [[UIButton alloc] initWithFrame: CGRectMake (GAP, 30, IMG_VIEW_WIDTH, IMG_VIEW_HEIGHT)]; [serial_btn setTitle: @ "serial loading" forState: UIControlStateNormal]; [serial_btn setBackgroundColor: [UIColor purpleColor]; [self. view addSubview: serial_btn]; [serial_btn addTarget: self action: @ selector (listener) forControlEvents: Listener]; 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: Unknown]; [global_btn setBackgroundColor: [UIColor purpleColor]; [self. view addSubview: global_btn]; [global_btn addTarget: self action: @ selector (loadImage_gcd_global) forControlEvents: UIControlEventTouchUpInside];} # pragma mark-loading photos from the network in a serial way) loadImage_gcd_serial {// serial queue dispatch_queue_t serialQueue = dispatch_queue_create ("myThreadQueue1", queue); // request image for (int I = 0; I <10; I ++) {dispatch_async (serialQueue, ^ {// obtain image NSURL * url = [NSURL URLWithString: @ "http://www.jerehedu.com/images/temp/logo.gif"]; NSData * data = [NSData dataWithContentsOfURL]; UIImage * image = [UIImage imageWithData: data]; // return to the master thread to refresh the interface dispatch_sync (dispatch_get_main_queue (), ^ {UIImageView * currentImgView = (UIImageView *) [self. view viewWithTag: I + 1]; currentImgView. image = image ;}) ;}}# pragma mark-parallel loading of photos from the network-(void) loadImage_gcd_global {// parallel queue dispatch_queue_t globalQueue = dispatch_get_global_queue (queue, 0); // request image for (int I = 0; I <10; I ++) {dispatch_async (globalQueue, ^ {// obtain image NSURL * url = [NSURL URLWithString: @ "http://www.jerehedu.com/images/temp/logo.gif"]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * image = [UIImage imageWithData: data]; // return to the master thread to refresh the interface dispatch_sync (dispatch_get_main_queue (), ^ {UIImageView * currentImgView = (UIImageView *) [self. view viewWithTag: I + 1]; currentImgView. image = image;}) ;}}}- (void) didReceiveMemoryWarning {[super didReceiveMemoryWarning];} @ end

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

Related Article

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.