GCD
2017-05-19 本文已影响0人
小的小碰撞
- 获得主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
- 获得全局队列
dispatch_queue_t gloableQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- 串行队列
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
- 异步队列
dispatch_async(queue, ^{
});
- 同步队列
dispatch_sync(queue, ^{
});
- 阻隔队列 (像栅栏一样 ) 例如 A -->栅栏 --> B 有序
dispatch_barrier_async(queue, ^{
NSLog(@"----barrier-----%@", [NSThread currentThread]);
});
- eg :异步线程加载图片
-(void)asyncSetImage{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
// 加载图片
NSData *data = [NSData dataWithContentsOfURL:url];
//生成图片
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
});
}