炎黄神途,香菇价格,街头篮球辅助yy
gcd的基本使用
一、主队列介绍
主队列:是和主线程相关联的队列,主队列是gcd自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行。
提示:如果把任务放到主队列中进行处理,那么不论处理函数是异步的还是同步的都不会开启新的线程。
获取主队列的方式:
(1)使用异步函数执行主队列中得任务,代码示例:
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@end
- (void)viewdidload
{
[super viewdidload];
//打印主线程
nslog(@"打印主线程--%@", [nsthread mainthread]);
//1.获取主队列
dispatch_queue_t queue=dispatch_get_main_queue();
//2.把任务添加到主队列中执行
dispatch_async(queue, ^{
nslog(@"使用异步函数执行主队列中的任务1--%@",[nsthread currentthread]);
});
dispatch_async(queue, ^{
nslog(@"使用异步函数执行主队列中的任务2--%@",[nsthread currentthread]);
});
dispatch_async(queue, ^{
nslog(@"使用异步函数执行主队列中的任务3--%@",[nsthread currentthread]);
});
}
@end
(2)使用同步函数,在主线程中执行主队列中得任务,会发生死循环,任务无法往下执行。示意图如下:
二、基本使用
1.问题
任务1和任务2是在主线程执行还是子线程执行,还是单独再开启一个新的线程?
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@end
- (void)viewdidload
{
[super viewdidload];
//开启一个后台线程,调用执行test方法
[self performselectorinbackground:@selector(test) withobject:nil];
}
-(void)test
{
nslog(@"当前线程---%@",[nsthread currentthread]);
dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//异步函数
dispatch_async(queue, ^{
nslog(@"任务1所在的线程----%@",[nsthread currentthread]);
});
//同步函数
dispatch_sync(queue, ^{
nslog(@"任务2所在的线程----%@",[nsthread currentthread]);
});
}
@end
2.开启子线程,加载图片
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@property (weak, nonatomic) iboutlet uiimageview *imageview;
@end
- (void)viewdidload
{
[super viewdidload];
}
//当手指触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
//1.获取一个全局串行队列
dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//2.把任务添加到队列中执行
dispatch_async(queue, ^{
//打印当前线程
nslog(@"%@",[nsthread currentthread]);
//3.从网络上下载图片
nsurl *urlstr=[nsurl urlwithstring:@"http://h.hiphotos.baidu.com/baike/w%3d268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
nsdata *data=[nsdata datawithcontentsofurl:urlstr];
uiimage *image=[uiimage imagewithdata:data];
//提示
nslog(@"图片加载完毕");
//4.回到主线程,展示图片
[self.imageview performselectoronmainthread:@selector(setimage:) withobject:image waituntildone:no];
});
}
@end
打印结果:
要求使用gcd的方式,在子线程加载图片完毕后,主线程拿到加载的image刷新ui界面。
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@property (weak, nonatomic) iboutlet uiimageview *imageview;
@end
- (void)viewdidload
{
[super viewdidload];
}
//当手指触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
//1.获取一个全局串行队列
dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//2.把任务添加到队列中执行
dispatch_async(queue, ^{
//打印当前线程
nslog(@"%@",[nsthread currentthread]);
//3.从网络上下载图片
nsurl *urlstr=[nsurl urlwithstring:@"http://h.hiphotos.baidu.com/baike/w%3d268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
nsdata *data=[nsdata datawithcontentsofurl:urlstr];
uiimage *image=[uiimage imagewithdata:data];
//提示
nslog(@"图片加载完毕");
//4.回到主线程,展示图片
// [self.imageview performselectoronmainthread:@selector(setimage:) withobject:image waituntildone:no];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageview.image=image;
//打印当前线程
nslog(@"%@",[nsthread currentthread]);
});
});
}
@end
好处:子线程中得所有数据都可以直接拿到主线程中使用,更加的方便和直观。
三、线程间通信
从子线程回到主线程
// 回到主线程,执⾏ui刷新操作
});
});
gcd的常见用法
一、延迟执行
1.介绍
ios常见的延时执行有2种方式
(1)调用nsobject的方法
// 2秒后再调用self的run方法
// 2秒后异步执行这里的代码...
});
2.说明
第一种方法,该方法在那个线程调用,那么run就在哪个线程执行(当前线程),通常是主线程。
代码示例:
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@end
- (void)viewdidload
{
[super viewdidload];
nslog(@"打印线程----%@",[nsthread currentthread]);
//延迟执行
//第一种方法:延迟3秒钟调用run函数
[self performselector:@selector(run) withobject:nil afterdelay:2.0];
}
-(void)run
{
nslog(@"延迟执行----%@",[nsthread currentthread]);
}
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
//在异步函数中执行
dispatch_queue_t queue = dispatch_queue_create("wendingding", 0);
dispatch_sync(queue, ^{
[self performselector:@selector(test) withobject:nil afterdelay:1.0];
});
nslog(@"异步函数");
}
-(void)test
{
nslog(@"异步函数中延迟执行----%@",[nsthread currentthread]);
}
@end
第二种方法,
//延迟执行的方法
});
参数说明:
什么时间,执行这个队列中的这个任务。
代码示例:
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@end
- (void)viewdidload
{
[super viewdidload];
nslog(@"打印当前线程---%@", [nsthread currentthread]);
//延迟执行,第二种方式
//可以安排其线程(1),主队列
dispatch_queue_t queue= dispatch_get_main_queue();
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(5.0 * nsec_per_sec)), queue, ^{
nslog(@"主队列--延迟执行------%@",[nsthread currentthread]);
});
//可以安排其线程(2),并发队列
//1.获取全局并发队列
dispatch_queue_t queue1= dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//2.计算任务执行的时间
dispatch_time_t when=dispatch_time(dispatch_time_now, (int64_t)(5.0 * nsec_per_sec));
//3.会在when这个时间点,执行queue中的这个任务
dispatch_after(when, queue1, ^{
nslog(@"并发队列-延迟执行------%@",[nsthread currentthread]);
});
}
@end
延迟执行:不需要再写方法,且它还传递了一个队列,我们可以指定并安排其线程。
如果队列是主队列,那么就在主线程执行,如果队列是并发队列,那么会新开启一个线程,在子线程中执行。
二、一次性代码
1.实现一次性代码
需求:点击控制器只有第一次点击的时候才打印。
实现代码:
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@property(nonatomic,assign) bool log;
@end
@implementation yyviewcontroller
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
if (_log==no) {
nslog(@"该行代码只执行一次");
_log=yes;
}
}
@end
2.使用dispatch_once一次性代码
使用dispatch_once函数能保证某段代码在程序运行过程中只被执行1次
dispatch_once(&oncetoken, ^{
// 只执行1次的代码(这里面默认是线程安全的)
});
代码示例:
#import "yyviewcontroller.h"
@interface yyviewcontroller ()
@property(nonatomic,assign) bool log;
@end
//-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
//{
// if (_log==no) {
// nslog(@"该行代码只执行一次");
// _log=yes;
// }
//}
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
nslog(@"该行代码只执行一次");
});
}
@end
三、队列组
需求:从网络上下载两张图片,把两张图片合并成一张最终显示在view上。
1.第一种方法
代码示例:
#import "yyviewcontroller.h"
//宏定义全局并发队列
#define global_quque dispatch_get_global_queue(dispatch_queue_priority_default, 0)
//宏定义主队列
#define main_queue dispatch_get_main_queue()
@interface yyviewcontroller ()
@property (weak, nonatomic) iboutlet uiimageview *imageview1;
@property (weak, nonatomic) iboutlet uiimageview *imageview2;
@property (weak, nonatomic) iboutlet uiimageview *imageview3;
@end
- (void)viewdidload
{
[super viewdidload];
}
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
//获取全局并发队列
// dispatch_queue_t queue= dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//获取主队列
// dispatch_queue_t queue= dispatch_get_main_queue();
// 图片1:http://d.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg
// 图片2:http://h.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg
dispatch_async(global_quque, ^{
//下载图片1
uiimage *image1= [self imagewithurl:@"http://d.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"];
nslog(@"图片1下载完成---%@",[nsthread currentthread]);
//下载图片2
uiimage *image2= [self imagewithurl:@"http://h.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
nslog(@"图片2下载完成---%@",[nsthread currentthread]);
//回到主线程显示图片
dispatch_async(main_queue, ^{
nslog(@"显示图片---%@",[nsthread currentthread]);
self.imageview1.image=image1;
self.imageview2.image=image2;
//合并两张图片
uigraphicsbeginimagecontextwithoptions(cgsizemake(200, 100), no, 0.0);
[image1 drawinrect:cgrectmake(0, 0, 100, 100)];
[image2 drawinrect:cgrectmake(100, 0, 100, 100)];
self.imageview3.image=uigraphicsgetimagefromcurrentimagecontext();
//关闭上下文
uigraphicsendimagecontext();
nslog(@"图片合并完成---%@",[nsthread currentthread]);
});
//
});
}
//封装一个方法,传入一个url参数,返回一张网络上下载的图片
-(uiimage *)imagewithurl:(nsstring *)urlstr
{
nsurl *url=[nsurl urlwithstring:urlstr];
nsdata *data=[nsdata datawithcontentsofurl:url];
uiimage *image=[uiimage imagewithdata:data];
return image;
}
@end
打印查看:
问题:这种方式的效率不高,需要等到图片1.图片2都下载完成后才行。
提示:使用队列组可以让图片1和图片2的下载任务同时进行,且当两个下载任务都完成的时候回到主线程进行显示。
2.使用队列组解决
步骤:
创建一个组
开启一个任务下载图片1
开启一个任务下载图片2
同时执行下载图片1\下载图片2操作
等group中的所有任务都执行完毕, 再回到主线程执行其他操作
代码示例
#import "yyviewcontroller.h"
//宏定义全局并发队列
#define global_quque dispatch_get_global_queue(dispatch_queue_priority_default, 0)
//宏定义主队列
#define main_queue dispatch_get_main_queue()
@interface yyviewcontroller ()
@property (weak, nonatomic) iboutlet uiimageview *imageview1;
@property (weak, nonatomic) iboutlet uiimageview *imageview2;
@property (weak, nonatomic) iboutlet uiimageview *imageview3;
@end
- (void)viewdidload
{
[super viewdidload];
}
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
// 图片1:http://d.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg
// 图片2:http://h.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg
//1.创建一个队列组
dispatch_group_t group = dispatch_group_create();
//2.开启一个任务下载图片1
__block uiimage *image1=nil;
dispatch_group_async(group, global_quque, ^{
image1= [self imagewithurl:@"http://d.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"];
nslog(@"图片1下载完成---%@",[nsthread currentthread]);
});
//3.开启一个任务下载图片2
__block uiimage *image2=nil;
dispatch_group_async(group, global_quque, ^{
image2= [self imagewithurl:@"http://h.hiphotos.baidu.com/baike/c0%3dbaike80%2c5%2c5%2c80%2c26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
nslog(@"图片2下载完成---%@",[nsthread currentthread]);
});
//同时执行下载图片1\下载图片2操作
//4.等group中的所有任务都执行完毕, 再回到主线程执行其他操作
dispatch_group_notify(group,main_queue, ^{
nslog(@"显示图片---%@",[nsthread currentthread]);
self.imageview1.image=image1;
self.imageview2.image=image2;
//合并两张图片
//注意最后一个参数是浮点数(0.0),不要写成0。
uigraphicsbeginimagecontextwithoptions(cgsizemake(200, 100), no, 0.0);
[image1 drawinrect:cgrectmake(0, 0, 100, 100)];
[image2 drawinrect:cgrectmake(100, 0, 100, 100)];
self.imageview3.image=uigraphicsgetimagefromcurrentimagecontext();
//关闭上下文
uigraphicsendimagecontext();
nslog(@"图片合并完成---%@",[nsthread currentthread]);
});
}
-(void)download2image
{
//获取全局并发队列
// dispatch_queue_t queue= dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//获取主队列
// dispatch_queue_t queue= dispatch_get_main_queue();
dispatch_async(global_quque, ^{
//下载图片1
uiimage *image1= [self imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
nslog(@"图片1下载完成---%@",[nsthread currentthread]);
//下载图片2
uiimage *image2= [self imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
nslog(@"图片2下载完成---%@",[nsthread currentthread]);
//回到主线程显示图片
dispatch_async(main_queue, ^{
nslog(@"显示图片---%@",[nsthread currentthread]);
self.imageview1.image=image1;
self.imageview2.image=image2;
//合并两张图片
uigraphicsbeginimagecontextwithoptions(cgsizemake(200, 100), no, 0.0);
[image1 drawinrect:cgrectmake(0, 0, 100, 100)];
[image2 drawinrect:cgrectmake(0, 0, 100, 100)];
self.imageview3.image=uigraphicsgetimagefromcurrentimagecontext();
//关闭上下文
uigraphicsendimagecontext();
nslog(@"图片合并完成---%@",[nsthread currentthread]);
});
//
});
}
//封装一个方法,传入一个url参数,返回一张网络上下载的图片
-(uiimage *)imagewithurl:(nsstring *)urlstr
{
nsurl *url=[nsurl urlwithstring:urlstr];
nsdata *data=[nsdata datawithcontentsofurl:url];
uiimage *image=[uiimage imagewithdata:data];
return image;
}
@end
2.补充说明
有这么1种需求:
首先:分别异步执行2个耗时的操作
其次:等2个异步操作都执行完毕后,再回到主线程执行操作
如果想要快速高效地实现上述需求,可以考虑用队列组
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(), ^{
// 等前面的异步操作都执行完毕后,回到主线程...
});
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
iOS 使用UITextField自定义搜索框 实现用户输入完之后“实时搜索”功能
网友评论