知識(shí)點(diǎn)一
GCD中還有個(gè)用來執(zhí)行任務(wù)的函數(shù)
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
在前面的任務(wù)執(zhí)行幾位數(shù)后它才執(zhí)行息楔,而且它后面的任務(wù)等它執(zhí)行完成之后才會(huì)執(zhí)行
這個(gè)queue不能是全局的并發(fā)隊(duì)列
知識(shí)點(diǎn)二
延時(shí)執(zhí)行
iOS常見的延時(shí)執(zhí)行方法
1.調(diào)用NSObject方法
[self performSelector:@selector(run)? WithObject:nil afterDelay:2.0];
//2秒后再延遲self 的 run方法
2.使用GCD函數(shù)
dispatch_ater(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
//2秒后再執(zhí)行這里的代碼
})
3.使用NSTimer
[NSTimer scheduledTimerWithTimerInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:NO];
知識(shí)點(diǎn)三
一次性代碼
使用dispatch_once函數(shù)能保證某段代碼在程序運(yùn)行過程中只執(zhí)行1次
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
//只能執(zhí)行1次的代碼(這里面默認(rèn)是線程安全的)
NSLog(@"-------------run");
});
知識(shí)點(diǎn)四
快速迭代
使用dispatch_apply函數(shù)能進(jìn)行快速迭代遍歷
dispatch_apply(10,queue,^(size_t index){
//執(zhí)行10次代碼,index順序不確定
NSLog(@"---------------%ld--------%@",index,[NSThread currentThread]);
});
知識(shí)點(diǎn)五
隊(duì)列組
有這么種需求
首先,分別異步執(zhí)行2個(gè)耗時(shí)的操作
其次:等2個(gè)異步操作都執(zhí)行完畢后,再回到主線程執(zhí)行操作
如果想要快速高效地實(shí)現(xiàn)上述需求净嘀,可以考慮用隊(duì)列組
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
//執(zhí)行1個(gè)耗時(shí)的異步操作
});
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
//執(zhí)行1個(gè)耗時(shí)的異步操作
});
dispatch_group_notify(group,dispatch_get_main_queue(),^{
//等前面的異步操作都執(zhí)行完畢后窿锉,回到主線程。澄阳。括授。倾哺。
});
代碼實(shí)現(xiàn)部分
//圖片1
@property (nonatomic, strong) UIImage *image1;
//圖片2
@property (nonatomic, strong) UIImage *image2;
@property(weak,nonatomic) IBOutlet UIImageView *imageView;
//異步函數(shù)要等整個(gè)函數(shù)執(zhí)行完了才執(zhí)行NSLog(@"-------轧邪?--------%@",[NSThread currentThread]);也就是NSLog(@"----------touchesBegan-------------");執(zhí)行完才打印NSLog(@"-------?--------%@",[NSThread currentThread]);
//同步函數(shù)立即執(zhí)行NSLog(@"-------羞海?--------%@",[NSThread currentThread]);
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//[self barrier];
//[self delay];
//[self once];
//[self apply];
//[self group];
//dispatch_async(dispatch_queue_t queue,^(void)block);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_PRIORITY_QUEUE,0);
dispatch_async_f(queue,NUll ,download);
}
void download(void *data){
}
- (void)group{
//1.下載圖片1
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
//創(chuàng)建一個(gè)隊(duì)列組
diapatch_group_t group? = dispatch_group_create();
dispatch_group_async(group?,^{
//圖片的網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:@"http://img-arch.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//加載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
//生成圖片
sefl.image1 = [UIImage imageWithData:data];
});
//2.下載圖片2
dispatch_group? _async(group?,^{
//圖片的網(wǎng)絡(luò)路徑
NSURL *url = [USURL URLWithString:@http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
//加載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
//生成圖片
sefl.image2 = [UIImage imageWithData:data];
});
//3.將圖片1却邓、圖片2合成一張新的圖片
//dispatch_barrier_async(queue,^{});//前面線程要自己創(chuàng)建線程,不用global線程
//將前面執(zhí)行完了腊徙,后面才執(zhí)行
dispatch_group_notify(group,queue,^{
//NSLog(@"%@ %@",self.image1,self.image2);
//開啟新的圖形上下文
UIGraphicsBeginImageContext(CGSizeMake(100,100));
//繪制圖片
[self.image1 drawInRect:CGRectMake(0,0,50,100)];
[self.image2 drawInRect:CGRectMake(50,0,50,100)];
//獲得上下文中的圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束上下文中的圖片
UIGraphicsEndImageContext();
//回到主線程顯示圖片
dispatch_async(dispatch_get_main_queue(), ^{
//4.將新圖片顯示出來
//在storyboard拖拽UIImageView 并且綁定屬性
self.imageView.image = image;
});
});
}
快速迭代
- (void)apply{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
NSString *from = @"/Users/xiaomage/Desktop/From";//文件夾路徑
NSString *to?= @"/Users/xiaomage/Desktop/To";//另外個(gè)文件夾的路徑
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *subpaths = [manager subpathsAtPath:from];
//快速迭代:同時(shí)執(zhí)行
//可以實(shí)現(xiàn)循環(huán)
dispatch_apply(10,queue,^(size_t index){
NSString *subpath = subpaths[index];
NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
//剪切
[manager moveItemAtPath:fromFullpath toPath:toFullpath error:nil];
NSLog(@====%@-----%@,subpath,[NSThread currentThread]);
});
}
保證代碼只執(zhí)行一次(整個(gè)程序運(yùn)行過程中只執(zhí)行一次,懶加載中不能使用dispatch_once)
- (void)once{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
NSLog(@"-------------run");
});
}
- (void)run{
NSLog(@"-----------------run");
}
//延遲執(zhí)行方法
- (void)delay{
NSLog(@"touchesBegan----------------");
//延遲的第一種方法
//[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
//第二種方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
NSLog(@"run------------");
});
//第三種方法
[NSTimer scheduledTimeWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
}
//阻礙執(zhí)行方法
- (void)barrier
{
for(NSInteger i = 0; i < 10; i++){
dispatch_queue_t queue? = dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue,^{
NSLog(@"-------1--------%@",[NSThread currentThread]);
});
dispatch_async(queue,^{
NSLog(@"-------2--------%@",[NSThread currentThread]);
});
//barrier的作用:barrier前的任務(wù)先執(zhí)行檬某,barrier后的任務(wù)后執(zhí)行
dispatch_barrier_async(queue,^{
NSLog(@"---------------barrier----------%@",[NSThread currentThread]);
});
dispatch_async(queue,^{
NSLog(@"-------3--------%@",[NSThread currentThread]);
});
}
NSLog(@"----------touchesBegan-------------");
}
//延時(shí)執(zhí)行
- (void)