//延遲執行
-(void)delay
{
NSLog(@"start-----");
//1.延遲執行的第一種方法
//[self performSelector:@selector(task) withObject:nil afterDelay:2.0];
//2.延遲執行的第二種方法
//[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_tqueue =dispatch_get_global_queue(0,0);
/*
第一個參數:DISPATCH_TIME_NOW從現在開始計算時間
第二個參數:延遲的時間2.0 GCD時間單位:納秒
第三個參數:隊列
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)), queue, ^{
NSLog(@"GCD----%@",[NSThreadcurrentThread]);
});
}
//一次性代碼
//不能放在懶加載中的,應用場景:單例模式
-(void)once
{
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
NSLog(@"---once----");
});
}