GCD是苹果开发的一个多核编程的解决方法。Grand Central Dispatch(GCD)在MacOS X10.6(雪豹)中首次推出,并随后被引入到了iOS4.0中。GCD和其他的多线程技术方案,如NSThread、NSOperationQueue、NSInvocationOperation等技术相比,使用起来更加方便。
先看一个编程场景,在iPhone上做一个下载网页的功能,该功能非常简单,就是在iPhone上放置一个按钮,单击按钮时,显示一个转动的圆圈,表示正在下载,下载完成之后,将内容加载到界面上的一个文件控件中。
(1)使用GCD之前
虽然功能简单,但是我们必须吧下载过程放到后台线程中,否则会阻塞UI线程显示。如果不用GCD,我们需要这么写:
// Created by cuzZLYues on 2017/7/6.
// Copyright © 2017年 cuzZLYues. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD.h"
@interface ViewController ()
/** */
@property (weak, nonatomic) IBOutlet UITextView *content;
@property (nonatomic,strong) UIButton *myBtn;
/** */
//@property (nonatomic,strong) UILabel *content;
@end
static NSOperationQueue * queue;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
************* 设置一个按钮 *******
*/
_myBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_myBtn.frame = CGRectMake(100, 50, 200, 100);
[_myBtn setTitle:@"开始下载" forState:UIControlStateNormal];
_myBtn.titleLabel.font = [UIFont systemFontOfSize:24];
[_myBtn addTarget:self action:@selector(someClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_myBtn];
}
//单击按钮后调用
-(void)someClick:(id)sender{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
/*
我们用NSInvocationOperation建了一个后台线程,并放到NSOperationQueue中。后台线程执行download方法。
*/
queue = [[NSOperationQueue alloc]init];
NSInvocationOperation * op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
[queue addOperation:op];
}
/*
这个方法处理下载网页的逻辑,下载完成后用performSelectorOnMainThread执行download_completed方法
*/
-(void)download{
NSURL * url = [NSURL URLWithString:@"http://www.youdao.com"];
NSError * error;
//这里用的是NSString,如果是复杂的应用就可能不能这么简单了
NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (data!=nil) {
[self performSelectorOnMainThread:@selector(download_completed:) withObject:data waitUntilDone:NO];
}else{
NSLog(@"error when download:%@",error);
}
}
-(void)download_completed:(NSString *)data{
NSLog(@"call back");
//这个方法进行一个clear up 工作,停止菊花转动,并且把下载的内容显示在textView上面。
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.content.text = data;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果也可以达到,接下来见证一下GCD的强大:
// Created by cuzZLYues on 2017/7/6.
// Copyright © 2017年 cuzZLYues. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD.h"
@interface ViewController ()
/** */
@property (weak, nonatomic) IBOutlet UITextView *content;
@property (nonatomic,strong) UIButton *myBtn;
@end
static NSOperationQueue * queue;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
************* 设置一个按钮 *******
*/
_myBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_myBtn.frame = CGRectMake(100, 50, 200, 100);
[_myBtn setTitle:@"开始下载" forState:UIControlStateNormal];
_myBtn.titleLabel.font = [UIFont systemFontOfSize:24];
[_myBtn addTarget:self action:@selector(someClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_myBtn];
}
//单击按钮后调用
-(void)someClick:(id)sender{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[self useGCD];
}
-(void)useGCD{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://www.youdao.com"];
NSError * error;
//这里用的是NSString,如果是复杂的应用就可能不能这么简单了
NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (data!= nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.content.text = data;
});
}else{
NSLog(@"error when download:%@",error);
}
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
首先我们可以看到,代码变短了。因为少了原来三个方法,也少了相互之间需要传递的变量的封装。
另外,代码变的非常的清晰,虽然是异步的代码,但是被GCD很牛逼的整合到了一起,逻辑清晰。如果应用MVC,我们也可以将ViewController 层的回调函数用GCD的方式传递给Model层,这相比以前用的@selector的方式,代码的逻辑关系更加的清楚。
如果有什么问题可以下载源码研究一下:
https://github.com/yueShenAAA/UseGCDDemo
(1)block的定义
简单block的定义有点像函数指针,差别是用‘^’代替函数指针‘*’符号:
//声明变量
void (^loggerBlcok)(void);
//定义
loggerBlcok = ^{
//code
};
//调用
loggerBlcok();
但是大多数情况下,我们时候内联的方式来定义它,即将它的程序块写在调用的函数里面,例如:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//code do something
});
从上面可以看的出来,block有一下两点特点:
1.程序块可以在代码中以内联的方式来定义。
2.程序块可以访问在创建它的范围内的可用变量。
(2)系统提供的dispatch方法
为了方便的使用GCD,苹果提供了一些方法方便我们将block放在主线程上后台执行,或者延后执行:
//后台执行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//code do something
});
//主线程执行:
dispatch_async(dispatch_get_main_queue(), ^{
//code do something
});
//一次性执行(单例的写法):
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//code to be executed once
});
//延迟2两秒执行:
double delayInSecond = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSecond*NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
//code to be executed on the main queue after delay
});
dispatch_queue_t 也可以自己定义,如要自定义queue,可以用dispatch_queue_create方法:
// 自定义queue
dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
dispatch_async(urls_queue, ^{
// code
});
另外,GCD还有一些高级用法,例如让后台两个线程并行执行,然后等两个线程都结束后,再汇总执行结果。这个可以用dispatch_group、dispatch_group_async和dispatch_group_notify来实现:
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//并行执行的线程一
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//并行执行的线程二
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
//汇总结果
});
(3)修改block之外的变量
默认情况下,在程序中访问的外部变量是复制过去的,即写操作不对原变量生效。但是你可以加上__block来让其写操作生效:
__block int a = 0;
void (^foo)(void) = ^{
a = 1;
};
foo();
//这里的a的值被修改为1;
(4)后台运行
使用block的另一个用处是让程序在后台比较长久的运行。在以前,当应用被按home键退出后,应用仅有最多5秒的时间做一些保存或清理资源的工作。但是应用可以调用UIApplication的beginBackgroundTaskWithExpirationHandler方法,让应用最多有10分钟的时间在后台长久运行。这个时间可以用来做清理本地缓存、发送统计数据等工作。
让程序在后台长久运行的代码如下:
AppDelegate.h文件
// Created by cuzZLYues on 2017/7/6.
// Copyright © 2017年 cuzZLYues. All rights reserved.
//
#import
#import
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
/** */
@property (nonatomic,assign) UIBackgroundTaskIdentifier backgroudUpdateTask;
- (void)saveContext;
@end
AppDelegate.m文件
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self beginBackgroudUpdateTask];
//这里加上你需要长久运行的代码
[self endBackgroundUpdateTask];
NSLog(@"进入后台");
}
-(void)beginBackgroudUpdateTask{
self.backgroudUpdateTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
-(void)endBackgroundUpdateTask{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroudUpdateTask];
self.backgroudUpdateTask = UIBackgroundTaskInvalid;
NSLog(@"后台做事儿");
}
总体来说,GCD能够极大的方便开发者进行多线程编程,大家应该尽量使用GCD来处理后台线程和UI线程的交互。