IOSGCD Basic Usage

Source: Internet
Author: User

Sleepy, and sleep more. Run a little bit of welfare, understand. I do not upload here, we run the ha ... Good night



#import "ViewController.h"


@interface viewcontroller ()

{

uiimageview *_view;

}

@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];


// The Home row is a queue that is associated with the main thread, and the host column is a special queue that GCD provides, adding tasks to the main queue, Executes in the main thread. Note: Adding a task to the primary queue, either synchronously or asynchronously, does not create a thread.

//Dispatch_get_main_queue (); method to fetch to the home row

// turn on child thread call Test Method

[selfperformselectorinbackground:@selector(test) withobject:nil];

_view = [[uiimageviewalloc] Init];

_view. Frame = CGRectMake (0,0, self. ) View . bounds . size . width , - );

[self. View addsubview:_view];

}


-(void) test

{

dispatch_queue_t queue =dispatch_queue_create(" Concurrent Queue ",dispatch_queue_concurrent);

dispatch_async(queue, ^{

NSLog(@ "222 Curthread =%@", [nsthreadCurren TThread]);

});

dispatch_sync(queue, ^{

NSLog(@ "333 curethread =%@", [nsthreadCurrentt Hread]);

});

}




// The user taps the screen, downloads a picture from the network, and displays it to the interface.

-(void) Touchesbegan: ( Nsset *) touches withevent: (uievent *) Event

{

//1. Take global Queue

dispatch_queue_t queue =dispatch_get_global_queue( Dispatch_queue_priority_default,0);

// Add the task of downloading pictures to the parallel queue

dispatch_async(queue, ^{

NSLog(@ "1111:curthread =%@", [nsthread CurrentThread ]);

nsurl *url = [nsurlurlwithstring:@ "Http://h.hi Photos.baidu.com/image/pic/item/35a85edf8db1cb13966db40fde54564e92584ba2.jpg "];

nsdata *data = [nsdatadatawithcontentsofurl :url];

UIImage *image = [UIImageimagewithdata:d ATA];

// requires GCD to set the picture to be displayed in the main thread.

// benefits : in the main thread, you can use the resources in the child threads directly. Easy to use, intuitive.

// operation UI is executed in the main thread

dispatch_async(dispatch_get_main_queue(), ^{

_view. Image = image;

});

});

}



-(void) test11

{

//dispatch_queue_t queue = Dispatch_get_global_queue (< #long Identifier#>, < #unsigned long flags#>)

dispatch_queue_t queue =dispatch_queue_create("Concurrent queue" ,dispatch_queue_concurrent);

dispatch_async(queue, ^{

// do time-consuming operation

dispatch_async(dispatch_get_main_queue(), ^{

// operating UI interface

});

});

}

@end


IOSGCD Basic Usage

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.