////VIEWCONTROLLER.M//09-Mastering-GCD Common functions////Created by Xiaomage on 16/2/18.//2016 little brother. All rights reserved.//#import "ViewController.h"#import "XMGPerson.h"@interfaceViewcontroller ()@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//[self once];Xmgperson*P1 =[[Xmgperson alloc]init]; Xmgperson*P2 =[[Xmgperson alloc]init]; NSLog (@"%@---%@", p1.books,p2.books);}//deferred execution-(void) delay{NSLog (@"Start-----"); //1. The first method of deferred execution//[Self Performselector: @selector (Task) Withobject:nil afterdelay:2.0]; //2. Second method of deferred execution//[Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (Task) Userinfo:nil Repeats:yes]; //3.GCD//dispatch_queue_t queue = Dispatch_get_main_queue ();dispatch_queue_t queue = Dispatch_get_global_queue (0,0); /*First parameter: Dispatch_time_now calculate time from now the second parameter: Delay time 2.0 GCD time unit: nanosecond third parameter: Queue*/Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), queue, ^{NSLog (@"GCD----%@", [Nsthread CurrentThread]); });}//Disposable Code//can not be placed in lazy loading, application scenario: Singleton mode-(void) once{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{//the entire project life cycle is performed only once, and multiple calls are not executed more than onceNSLog (@"---once----"); });}-(void) task{NSLog (@"Task----%@", [Nsthread CurrentThread]);}@end
// // XMGPerson.h// 09-Mastering-GCD Common functions //// Created by Xiaomage on 16/2/18. // 2016 little brother. All rights reserved. // #import <Foundation/Foundation.h>@interface*books; @end
////xmgperson.m//09-Mastering-GCD Common functions////Created by Xiaomage on 16/2/18.//2016 little brother. All rights reserved.//#import "XMGPerson.h"@implementationXmgperson-(Nsarray *) books{//if (_books = = nil) {//_books = @[@ "1234", @ "56789"];// } Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{//Execute only once_books = @[@"1234",@"56789"]; }); return_books;}@end
IOS34---gdc,dispatch_once