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