一.線程間通信
從子線程回到主線程
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執(zhí)行耗時(shí)的異步操作...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主線程击蹲,執(zhí)行UI刷新操作
});
});
簡(jiǎn)單小demo:
//1.開線程下載圖片
//DISPATCH_QUEUE_PRIORITY_DEFAULT 0
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"%@----",[NSThread currentThread]);
//1.1 確定url
NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/blog/201308/24/20130824215734_ut8LZ.thumb.600_0.jpeg"];
//1.2 下載二進(jìn)制數(shù)據(jù)到本地
NSData *data = [NSData dataWithContentsOfURL:url];
//1.3 轉(zhuǎn)換圖片
UIImage *image = [UIImage imageWithData:data];
//刷新UI
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"%@--UI--",[NSThread currentThread]);
});
});
二.GCD常用函數(shù)
//延遲執(zhí)行
-(void)delay
{
NSLog(@"---start----");
//1
//[self performSelector:@selector(task) withObject:nil afterDelay:2.0];
//2.
//[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(task) userInfo:nil repeats:NO];
//3.
/*
第一個(gè)參數(shù):DISPATCH_TIME_NOW 從什么時(shí)候開始計(jì)時(shí)
第二個(gè)參數(shù):延遲的時(shí)間 2.0表示2秒 GCD的時(shí)間是以納秒為單位
第三個(gè)參數(shù):隊(duì)列
dispatch_get_main_queue() 主線程
如何是其他隊(duì)列(并發(fā)|串行),那么block在子線程中調(diào)用
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
NSLog(@"--GCD----%@",[NSThread currentThread]);
});
}
//柵欄函數(shù)
//知識(shí)點(diǎn):barrier在使用的時(shí)候不能使用全局并發(fā)隊(duì)列
-(void)barrier
{
//1.創(chuàng)建隊(duì)列
dispatch_queue_t queue = dispatch_queue_create("com.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download1---%zd--%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download2---%zd--%@",i,[NSThread currentThread]);
}
});
//1.在子線程中執(zhí)行
//2.開始執(zhí)行之前確保前面的任務(wù)1和任務(wù)2都已經(jīng)執(zhí)行完畢
//3.只有當(dāng)我執(zhí)行完畢之后才能繼續(xù)執(zhí)行后面的任務(wù)
dispatch_barrier_async(queue, ^{
NSLog(@"+++++++++++++%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download3---%zd--%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download4---%zd--%@",i,[NSThread currentThread]);
}
});
// dispatch_release(queue);
}
//一次性代碼
/*
1)整個(gè)應(yīng)用程序中只會(huì)執(zhí)行一次
2)本身是線程安全的
*/
-(void)once
{
NSLog(@"------%s",__func__);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"--once---%@",[NSThread currentThread]);
});
}
//快速迭代
-(void)apply
{
//串行執(zhí)行
// for (NSInteger i = 0; i<10; i++) {
// NSLog(@"%zd---%@",i,[NSThread currentThread]);
// }
/*
第一個(gè)參數(shù):迭代的次數(shù)
第二個(gè)參數(shù):隊(duì)列 !!!!不能傳主隊(duì)列
注意:主線程也會(huì)參與迭代的過程,里面的任務(wù)是并發(fā)執(zhí)行的
*/
dispatch_queue_t queue = dispatch_queue_create("download", DISPATCH_QUEUE_SERIAL);
// dispatch_get_global_queue(0, 0)
dispatch_apply(10, queue, ^(size_t index) {
NSLog(@"%zd---%@",index,[NSThread currentThread]);
});
}
-(void)moveFile
{
//1.確定文件路徑
NSString *sourePath = @"/Users/xiaomage/Desktop/from";
//2.確定文件應(yīng)該剪切到哪個(gè)目錄
NSString *targetPath = @"/Users/xiaomage/Desktop/to";
//3.得到所有的文件
NSArray *subpaths = [[NSFileManager defaultManager] subpathsAtPath:sourePath];
//4.執(zhí)行迭代.把所有的文件都剪切到指定的地方
dispatch_apply(subpaths.count, dispatch_get_global_queue(0, 0), ^(size_t index) {
NSString *fileName = subpaths[index];
//4.1 拼接文件的全路徑
來源目錄根文件全路徑
//stringByAppendingPathComponent /
NSString *soureFullpath = [sourePath stringByAppendingPathComponent:fileName];
//4.2 目的地
NSString *targetFullpath = [targetPath stringByAppendingPathComponent:fileName];
//4.3 執(zhí)行剪切操作
[[NSFileManager defaultManager] moveItemAtPath:soureFullpath toPath:targetFullpath error:nil];
NSLog(@"%@---%@--%@",soureFullpath,targetFullpath,[NSThread currentThread]);
});
}
-(void)task
{
NSLog(@"%s",__func__);
}
@end
隊(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í)行完畢后皮获,回到主線程...
});
還需補(bǔ)充.