GCD核心:
- 任务:执行什么操作
- 队列:用来存放任务
将任务添加到队列中
✓ GCD会自动将队列中的任务取出,放到对应的线程中执行
✓ 任务的取出遵循队列的FIFO原则:先进先出,后进后出 - GCD默认已经提供了全局的并发队列,供整个应用使用,不需要手动创建
dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);//手动创建创建一个线程,""以内是线程名称
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // GCD自带获得全局并发队列,有时候括号中可写(0,0);
- ■ 全局并发队列的优先级
- p #define DISPATCH_QUEUE_PRIORITY_HIGH 2 // 高
- p #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // 默认(中)
- p #define DISPATCH_QUEUE_PRIORITY_LOW (-2) // 低
- p #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // 后台
全部的形式
GCD常用方法
1.从子线程操作,回到主线程更新UI
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行耗时的异步操作...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线程,执行UI刷新操作
});
});
2.延时执行(把其他几种方法也写在上面)
调用NSObject的方法
1.[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
// 2秒后再调用self的run方法
使用GCD函数
2.dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 2秒后异步执行这里的代码,吃出主意执行的代码是在主线程中执行,这就取决于你最后面传入的线程
});
3.[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
3.只执行1次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 只执行1次的代码(这里面默认是线程安全的)
});
4.需求:分别异步执行2个耗时的操作,等2个异步操作都执行完毕后,再回到主线程执行操作
如果想要快速高效地实现上述需求,可以考虑用队列组
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步操作都执行完毕后,回到主线程...
});
#实例
- (void)group
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 创建一个队列组
dispatch_group_t group = dispatch_group_create();
// 1.下载图片1
dispatch_group_async(group, queue, ^{
// 图片的网络路径
NSURL *url = [NSURL URLWithString:@"http://www.apoints.com/graphics/UploadFiles/200803/20080301202754140.jpg"];
// 加载图片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成图片
self.image1 = [UIImage imageWithData:data];
});
// 2.下载图片2
dispatch_group_async(group, queue, ^{
// 图片的网络路径
NSURL *url = [NSURL URLWithString:@"http://img.taopic.com/uploads/allimg/120423/107913-12042323220753.jpg"];
// 加载图片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成图片
self.image2 = [UIImage imageWithData:data];
});
// 3.将图片1、图片2合成一张新的图片
dispatch_group_notify(group, queue, ^{
// 开启新的图形上下文
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
// 绘制图片
[self.image1 drawInRect:CGRectMake(0, 0, 100, 200)];
[self.image2 drawInRect:CGRectMake(100, 0, 100, 200)];
// 取得上下文中的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
// 回到主线程显示图片
dispatch_async(dispatch_get_main_queue(), ^{
// 4.将新图片显示出来
self.imageView.image = image;//如想测试,可自己创建一个imageView属性并显示在view上,frame设置(200,200)即可
});
});
}
5.需求,异步操作很多条数据,但是要求有些部分先执行,有些部分后执行
dispatch_barrier_async(<#dispatch_queue_t queue#>, <#^(void)block#>);
#实例
#此处需要特别注意,queue必须是创建的线程,如果是dispatch_get_global_queue则不行🚫
- (void)barrier
{
dispatch_queue_t queue = dispatch_queue_create("1234", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"1->%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2->%@", [NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier->%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3->%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"4->%@", [NSThread currentThread]);
});
}
下面是结果:栅栏前后部分依然是异步执行,但是一定是前面部分执行完毕了才会执行后面部分
第五种对应的打印结果
6.快速迭代,即(接近)同时执行
自己可以建两个文件夹,from,to,from里面有文件
/**
* 快速迭代,基本上同时执行
*/
- (void)apply
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSString *from = @"/Users/Guss/Desktop/from";
NSString *to = @"/Users/Guss/Desktop/to";
NSFileManager *mgr = [NSFileManager defaultManager];
NSArray *subpaths = [mgr subpathsAtPath:from];
dispatch_apply(subpaths.count, queue, ^(size_t index) {
NSString *subpath = subpaths[index];
NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
// 剪切
[mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil];
});
}
/**
* 传统文件剪切,遍历一个一个执行
*/
- (void)moveFile
{
NSString *from = @"/Users/Guss/Desktop/from";
NSString *to = @"/Users/Guss/Desktop/to";
NSFileManager *mgr = [NSFileManager defaultManager];
NSArray *subpaths = [mgr subpathsAtPath:from];
for (NSString *subpath in subpaths) {
NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 剪切
[mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil];
NSLog(@"%@---%@", [NSThread currentThread], subpath);
});
}
}