Use of GCD-related functions
GCD
It is one of iOS multi-thread implementation solutions and is very common.
It is also known as a great central scheduler.
Is a solution proposed by Apple for multi-core parallel computing
1. One-time function
Dispatch_once
As the name implies, a function is executed only once. Note that the whole program is executed only once (the singleton mode is commonly used)
-(Void) once {// One-time function. Only the static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {// default thread-safe NSLog (@ "------ run ");});}
2. fence Functions
Dispatch_barrier_async
The role is to control the execution sequence of multiple threads.
-(Void) barrier {dispatch_queue_t queue = dispatch_queue_create ("123", DISPATCH_QUEUE_CONCURRENT); dispatch_async (queue, ^ {NSLog (@ "_______ 1 -------- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "_______ 2 -------- % @", [NSThread currentThread]);}); // like a barrier, run dispatch_barrier_async (queue, ^ {NSLog (@ "---- barrier ----- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "_______ 3 -------- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "_______ 4 -------- % @", [NSThread currentThread]) ;});}
3. Fast iteration Functions
Dispatch_apply
The function is to enable multiple threads to complete one task at the same time, for example, downloading multiple images at the same time.
// General practice-(void) cutFromFileTo {// generally do this in the Child thread // create a parallel queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); dispatch_async (queue, ^ {// start path NSString * from = @ "/Users/DDZ/Desktop/From "; // target path NSString * to = @ "/Users/DDZ/Desktop/To"; NSFileManager * mgr = [NSFileManager defaultManager]; // obtain all the file paths in the starting path. NSArray * subpaths = [mgr subpathsAtPath: from]; for (int I = 0; I <subpaths. count; I ++) {// concatenate the path string NSString * fromFullPath = [NSString stringWithFormat: @ "% @/% @", from, subpaths [I]; NSString * toFullPath = [NSString stringWithFormat: @ "% @/% @", to, subpaths [I]; [mgr moveItemAtPath: fromFullPath toPath: toFullPath error: nil];} NSLog (@ "Cut successful ");});}View Code
// Use Quick Iteration for cutting-(void) cutFileApply {// start path NSString * from = @ "/Users/DDZ/Desktop/From "; // target path NSString * to = @ "/Users/DDZ/Desktop/To"; NSFileManager * mgr = [NSFileManager defaultManager]; // obtain all the file paths in the starting path. NSArray * subpaths = [mgr subpathsAtPath: from]; dispatch_apply (subpaths. count, dispatch_get_global_queue (queue, 0), ^ (size_t index) {NSString * subpath = subpaths [index]; // concatenate the path string NSString * fromFullPath = [NSString stringWithFormat: @ "% @/% @", from, subpath]; NSString * toFullPath = [NSString stringWithFormat: @ "% @/% @", to, subpath]; // cut [mgr moveItemAtPath: fromFullPath toPath: toFullPath error: nil] ;});}
Generally, only one image can be cut and the next image can be cut.
Fast iteration can be used for cutting at the same time.
4. Queue Group
Dispatch_group_async
For the same purpose as the barrier function, to control the execution sequence of multiple threads
For example, after downloading two images, merge the two into a new image and display it.
It must be completed before merging! (Multiple threads are uncontrollable due to order issues)
// Queue group-(void) group {// create a group dispatch_group_t group = dispatch_group_create (); dispatch_queue_t queue = dispatch_get_global_queue (queue, 0); // 1. download Picture 1 dispatch_group_async (group, queue, ^ {// download NSURL * url = [NSURL URLWithString: @ "http://g.hiphotos.baidu.com/image/pic/item/6c224f4a20a446230761b9b79c22720e0df3d7bf.jpg"]; NSData * data = [NSData dataWithContentsOfURL]; // generate image self. img1 = [UIImage imageWithData: data] ;}); // 2. download Image 2 dispatch_group_async (group, queue, ^ {// download NSURL * url = [NSURL URLWithString: @ "http://h.hiphotos.baidu.com/image/pic/item/b812c8fcc3cec3fd5b9db074d488d43f8794270b.jpg"]; NSData * data = [NSData dataWithContentsOfURL]; self. img2 = [UIImage imageWithData: data] ;}); // 3. combine image 1 and Image 2 into a new image dispatch_group_notify (group, queue, ^ {// enable the new image context UIGraphicsBeginImageContext (CGSizeMake (200,200 )); // draw the image [self. img1 drawInRect: CGRectMake (0, 0,100,200)]; [self. img2 drawInRect: CGRectMake (100, 0,100,200)]; // obtain the image UIImage * image in the context = UIGraphicsGetImageFromCurrentImageContext (); // end the context UIGraphicsEndImageContext (); // return to the main thread to display the image dispatch_async (dispatch_get_main_queue (), ^ {// 4. display the new image as self. imageView. image = image ;});});}View Code
5. latency (Supplement)
-(Void) delay {// NSLog (@ "______"); [self defined mselector: @ selector (run) withObject: nil afterDelay: 2.0];}-(void) run {NSLog (@ "end ");}