iOS常见的延迟执行有2种方式
(1)调用NSObject的方法
[self performSelector"@selector(run) withObject:nil afterDelay:2.0];
//2秒后再调用self的run 方法
(2)GCD函数
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int16_t)(2.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//2秒后异步执行这里的代码
});
2.说明
第一种方法,该方法在那个线程调用,那么run就在哪个线程执行(当前线程),通常是主线程。
- (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
说明:如果把该方法放在异步函数中执行,则方法不会被调用(BUG?)
第二种方法
- (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
延迟执行:不需要再写方法,且它还传递了一个队列,我们可以指定并安排其线程。
如果队列是主队列,那么就在主线程执行,如果队列是并发队列,那么会新开启一个线程,在子线程中执行。
//-(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(@"该行代码只执行一次"); }); }
1.注释中方法缺点,这是一个对象方法,当又创建了一个新的控制器,打印代码还是会执行(或者把状态开关作为本地存储,略繁琐,不推荐)
2.使用dispatch_once 一次性代码
需求:从网络上下载2个图片,把2张图片合并成1张最终显示再view上
#import "ViewController.h" //宏定义全局并发队列 #define global_quque dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //宏定义主队列 #define main_queue dispatch_get_main_queue() @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)]; [self.view addSubview:_imageView1]; _imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(105, 100, 100, 100)]; [self.view addSubview:_imageView2]; _imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 200, 100)]; [self.view addSubview:_imageView3]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { dispatch_async(global_quque, ^{ UIImage *image1 = [self imageWithUrl:@"http://d.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"]; NSLog(@"图片1下载完成---%@",[NSThread currentThread]); NSLog(@"_imageView1下载完成---%@",[NSThread currentThread]); UIImage *image2 = [self imageWithUrl:@"http://h.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"]; NSLog(@"图片2下载完成---%@",[NSThread currentThread]); NSLog(@"_imageView2下载完成---%@",[NSThread currentThread]); dispatch_async(main_queue, ^{ _imageView1.image =image1; _imageView2.image =image2; NSLog(@"显示图片---%@",[NSThread currentThread]); UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 1); [image1 drawInRect:CGRectMake(0, 0, 100, 100)]; [image2 drawInRect:CGRectMake(100, 0, 100, 100)]; _imageView3.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSLog(@"图片合并完成---%@",[NSThread currentThread]); }); }); } //封装一个方法,传入一个url参数,返回一张网络上下载的图片 -(UIImage *)imageWithUrl:(NSString *)urlStr { NSURL *url=[NSURL URLWithString:urlStr]; NSData *urlData = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:urlData]; return image; }
问题:这种方式效率不高,要等到图片下载完成之后 才行.
提示:使用队列组可以让图片1和图片2的下载任务同时进行,且当两个下载任务都完成的时候回到主线程进行显示。
使用组队来解决 (适用于2个耗时的操作,最后再回到主线程去执行)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //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]); _imageView1.image = image1; _imageView2.image=image2; //合并两张图片,最后一个参数是浮点数(0.0),不要写成0。 UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 1); [image1 drawInRect:CGRectMake(0, 0, 100, 100)]; [image2 drawInRect:CGRectMake(100, 0, 100, 100)]; _imageView3.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSLog(@"图片合并完成---%@",[NSThread currentThread]); }); }