GCD其他常用函数12

知识点一

GCD中还有个用来执行任务的函数

dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);

在前面的任务执行几位数后它才执行,而且它后面的任务等它执行完成之后才会执行

这个queue不能是全局的并发队列

知识点二

延时执行

iOS常见的延时执行方法

1.调用NSObject方法

[self performSelector:@selector(run)  WithObject:nil afterDelay:2.0];

//2秒后再延迟self 的 run方法

2.使用GCD函数

dispatch_ater(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{

//2秒后再执行这里的代码

})

3.使用NSTimer

[NSTimer scheduledTimerWithTimerInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:NO];

知识点三

一次性代码

使用dispatch_once函数能保证某段代码在程序运行过程中只执行1次

static dispatch_once_t onceToken;

dispatch_once(&onceToken,^{

//只能执行1次的代码(这里面默认是线程安全的)

NSLog(@"-------------run");

});

知识点四

快速迭代

使用dispatch_apply函数能进行快速迭代遍历

dispatch_apply(10,queue,^(size_t index){

//执行10次代码,index顺序不确定

NSLog(@"---------------%ld--------%@",index,[NSThread currentThread]);

});

知识点五

队列组

有这么种需求

首先,分别异步执行2个耗时的操作

其次:等2个异步操作都执行完毕后,再回到主线程执行操作

如果想要快速高效地实现上述需求,可以考虑用队列组

dispatch_group_t group = dispatch_group_create();

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(),^{

//等前面的异步操作都执行完毕后,回到主线程。。。。

});




代码实现部分

//图片1

@property (nonatomic, strong) UIImage *image1;

//图片2

@property (nonatomic, strong) UIImage *image2;

@property(weak,nonatomic) IBOutlet UIImageView *imageView;

//异步函数要等整个函数执行完了才执行NSLog(@"-------?--------%@",[NSThread currentThread]);也就是NSLog(@"----------touchesBegan-------------");执行完才打印NSLog(@"-------?--------%@",[NSThread currentThread]);

//同步函数立即执行NSLog(@"-------?--------%@",[NSThread currentThread]);

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//[self barrier];

//[self delay];

//[self once];

//[self apply];

//[self group];

//dispatch_async(dispatch_queue_t queue,^(void)block);

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_PRIORITY_QUEUE,0);

dispatch_async_f(queue,NUll ,download);



}

void download(void *data){

}


- (void)group{

//1.下载图片1

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

//创建一个队列组

diapatch_group_t group  = dispatch_group_create();

dispatch_group_async(group ,^{

//图片的网络路径

NSURL *url = [NSURL URLWithString:@"http://img-arch.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];

//加载图片

NSData *data = [NSData dataWithContentsOfURL:url];

//生成图片

sefl.image1 = [UIImage imageWithData:data];

});

//2.下载图片2

dispatch_group  _async(group ,^{

//图片的网络路径

NSURL *url = [USURL URLWithString:@http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];

//加载图片

NSData *data = [NSData dataWithContentsOfURL:url];

//生成图片

sefl.image2 = [UIImage imageWithData:data];

});


//3.将图片1、图片2合成一张新的图片

//dispatch_barrier_async(queue,^{});//前面线程要自己创建线程,不用global线程

//将前面执行完了,后面才执行

dispatch_group_notify(group,queue,^{

//NSLog(@"%@ %@",self.image1,self.image2);

//开启新的图形上下文

UIGraphicsBeginImageContext(CGSizeMake(100,100));

//绘制图片

[self.image1 drawInRect:CGRectMake(0,0,50,100)];

[self.image2 drawInRect:CGRectMake(50,0,50,100)];

//获得上下文中的图片

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

//结束上下文中的图片

UIGraphicsEndImageContext();

//回到主线程显示图片

dispatch_async(dispatch_get_main_queue(), ^{

//4.将新图片显示出来

//在storyboard拖拽UIImageView 并且绑定属性

self.imageView.image = image;

});


});



}

快速迭代

- (void)apply{

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

NSString *from = @"/Users/xiaomage/Desktop/From";//文件夹路径

NSString *to = @"/Users/xiaomage/Desktop/To";//另外个文件夹的路径

NSFileManager *manager = [NSFileManager defaultManager];

NSArray *subpaths = [manager subpathsAtPath:from];

//快速迭代:同时执行

//可以实现循环

dispatch_apply(10,queue,^(size_t index){

NSString *subpath = subpaths[index];

NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];

NSString *toFullpath = [to stringByAppendingPathComponent:subpath];

//剪切

[manager moveItemAtPath:fromFullpath toPath:toFullpath error:nil];

NSLog(@====%@-----%@,subpath,[NSThread currentThread]);

});

}


保证代码只执行一次(整个程序运行过程中只执行一次,懒加载中不能使用dispatch_once)

- (void)once{

static dispatch_once_t onceToken;

dispatch_once(&onceToken,^{

NSLog(@"-------------run");

});

}

- (void)run{

NSLog(@"-----------------run");

}

//延迟执行方法

- (void)delay{

NSLog(@"touchesBegan----------------");

//延迟的第一种方法

//[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

//第二种方法

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(2.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{

NSLog(@"run------------");

});

//第三种方法

[NSTimer scheduledTimeWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];

}

//阻碍执行方法

- (void)barrier

{

for(NSInteger i = 0; i < 10; i++){

dispatch_queue_t queue  = dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue,^{

NSLog(@"-------1--------%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"-------2--------%@",[NSThread currentThread]);

});

//barrier的作用:barrier前的任务先执行,barrier后的任务后执行

dispatch_barrier_async(queue,^{

NSLog(@"---------------barrier----------%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"-------3--------%@",[NSThread currentThread]);

});

}

NSLog(@"----------touchesBegan-------------");

}

//延时执行


- (void)

你可能感兴趣的:(GCD其他常用函数12)