GCD多线程操作
1)用得最多的操作
//获取全局队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//异步执行
dispatch_async(queue, ^{
//下载图片
dispatch_async(dispatch_get_main_queue(), ^{
//回到主线程更新UI
});
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"2222");
});
3)保证方法在整个程序只执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
4)组操作,多个请求,请求完成后再执行操作
//创建一个组,
dispatch_group_t group = dispatch_group_create();
//开启一个任务下载图片1
__block UIImage *image1 = nil;
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//image1 = [];
});
__block UIImage *image2 = nil;
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//image2 = [];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//image1
//image2
});
来源:https://www.cnblogs.com/LJYliu/p/5020642.html