标签:
GCD Grand Central Dispatch — — 伟大的中央调度器
核心: 将”任务” 放入” 队列”中,确定是同步执行还是异步执行
任务: 想要做的事情/ 执行什么操作/执行什么代码.
GCD中任务定义在block中
队列(FIFO): 用来存放任务
队列 != 线程
队列中的任务最后都要由线程来执行!
队列类型:
1> 串行 : Serial Dispatch Queue
one by one 一个任务执行完以后在执行下一个任务,
/*
@param label
A string label to attach to the queue.
This parameter is optional and may be NULL.
@param attr
DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT, or the result of a call to
the function dispatch_queue_attr_make_with_qos_class().
@result
The newly created dispatch queue.
*/
dispatch_queue_t serizlQueue
= dispatch_queue_create(“这是个串行队列(该队列的描述/附属物)", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue
= dispatch_queue_create(“这是个并行队列", DISPATCH_QUEUE_CONCURRENT);
/*!
* @function dispatch_get_main_queue
*
* @abstract
* Returns the default queue that is bound to the main thread.
*
* @discussion
* In order to invoke blocks submitted to the main queue, the application must
* call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
* thread.
*
* @result
* Returns the main queue. This queue is created automatically on behalf of
* the main thread before main() is called.
*/
dispatch_queue_t
dispatch_get_main_queue(void)
{
return DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q);
}
获取主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
获取全局并发队列(后边的参数默认为0)
dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
同步,异步
同步: 只能 在‘当前’线程 中执行任务,不 具备开启新的线程的能力
异步: 可以 在新的线程中执行任务, 具备开启新线程的能力
实例Code:
@interface SAMViewController ()
@property(nonatomic,strong) UIImageView *imageViews;
@property (nonatomic, strong) NSURL *url;
@end
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:self.url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageViews.image = image;
});
});
}
- (NSURL *)url{
if (_url == nil) {
NSString *str = @"http://img0.bdstatic.com/img/image/shouye/bizhi0701.jpg";
str = @"http://127.0.0.1/new/images/angle00.png";
_url = [NSURL URLWithString:str];
}
return _url;
}
- (UIImageView *)imageViews{
if (_imageViews == nil) {
_imageViews = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_imageViews];
}
return _imageViews;
}
常见的使用组合:
1> 异步函数 + 全局并发队列 (可以开启多条线程)
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 在这里执行耗时操作代码
dispatch_async(dispatch_get_main_queue(), ^{
// 在这里更新UI
});
});
2 > 异步函数 + 串行队列(自定义) 开启一条线程
dispatch_queue_t serizl = dispatch_queue_create("serizl", DISPATCH_QUEUE_SERIAL);
dispatch_async(serizl, ^{
// 执行耗时操作代码 (异步操作)
dispatch_async(dispatch_get_main_queue(), ^{
// 执行更新UI操作代码
});
});
常见延时执行方式
1>
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
});
2>
[self performSelector:@selector(testRun) withObject:nil afterDelay:2.0];
队列组的使用:
// 队列组的用途
// 执行一组耗时操作执行完后 再回到主线程中执行主线程任务
dispatch_group_t group = dispatch_group_create(); // 队列组
dispatch_queue_t queue = dispatch_get_global_queue(0, 0); // 全局并发队列
dispatch_group_async(group, queue, ^{ // 异步执行操作1
// longTime1
});
dispatch_group_async(group, queue, ^{ // 异步执行操作2
// longTime2
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 在主线程刷新数据
// reload Data
});
标签:
原文地址:http://www.cnblogs.com/wolfman4secret/p/4625339.html