当需要多个操作顺序执行,并且一次操作存在上限时候需要用到信号量(如批量下载,最多一次下载两个其他的等待)
一、不使用信号量
//第1个下载
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553747056229&di=7eec490de4bc8f8755681f84c72e5ea1&imgtype=0&src=http%3A%2F%2Fpic14.nipic.com%2F20110529%2F7570613_004640647181_2.jpg" count:1];
//第2个下载
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553746618224&di=fec695ff3469658cdb8431b11e07d5b8&imgtype=0&src=http%3A%2F%2Fpic31.nipic.com%2F20130630%2F7447430_165944650000_2.jpg" count:2];
//第3个下载
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553746639550&di=06727db028f00523f12571756525d600&imgtype=0&src=http%3A%2F%2Fbbswater-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg5%2FM00%2F0E%2F08%2FChMkJlrdXhCIHezjAAvWP5q8sbMAAn0iAA90kYAC9ZX715.jpg" count:3];
下载方法
- (void)downLoadWithUrlString:(NSString*)urlString count: (NSInteger)count
{
// 1.创建管理者对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.设置请求的URL地址
NSURL*url = [NSURLURLWithString:urlString];
// 3.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 4.下载任务
NSURLSessionDownloadTask*task = [managerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress) {
// 下载进度
NSLog(@"当前下载进度为:%lf",1.0* downloadProgress.completedUnitCount/ downloadProgress.totalUnitCount);
}destination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {
// 下载地址
NSLog(@"默认下载地址****************************:%@",targetPath);
// 设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
return[NSURLfileURLWithPath:filePath];// 返回的是文件存放在本地沙盒的地址
}completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {
// 下载完成调用的方法
NSLog(@"****************************: 第%li个文件\n****************************: 文件路径\n%@\n",count, filePath);
}];
// 5.启动下载任务
[taskresume];
}
下载结果是按照231的顺序下载的
二、使用信号量
//创建信号量2代表一次性最多执行两个操作,两个操作执行完之后在执行第三个操作
dispatch_semaphore_t sempone = dispatch_semaphore_create(2);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建任务1
dispatch_async(queue, ^{
dispatch_semaphore_wait(sempone, DISPATCH_TIME_FOREVER);
NSLog(@"run task 1");
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553747056229&di=7eec490de4bc8f8755681f84c72e5ea1&imgtype=0&src=http%3A%2F%2Fpic14.nipic.com%2F20110529%2F7570613_004640647181_2.jpg" count:1];
sleep(3);
NSLog(@"complete task 1");
dispatch_semaphore_signal(sempone);
});
//创建任务2
dispatch_async(queue, ^{
dispatch_semaphore_wait(sempone, DISPATCH_TIME_FOREVER);
NSLog(@"run task 2");
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553746618224&di=fec695ff3469658cdb8431b11e07d5b8&imgtype=0&src=http%3A%2F%2Fpic31.nipic.com%2F20130630%2F7447430_165944650000_2.jpg" count:2];
sleep(3);
NSLog(@"complete task 2");
dispatch_semaphore_signal(sempone);
});
//创建任务3
dispatch_async(queue, ^{
dispatch_semaphore_wait(sempone, DISPATCH_TIME_FOREVER);
NSLog(@"run task 3");
[self downLoadWithUrlString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553746639550&di=06727db028f00523f12571756525d600&imgtype=0&src=http%3A%2F%2Fbbswater-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg5%2FM00%2F0E%2F08%2FChMkJlrdXhCIHezjAAvWP5q8sbMAAn0iAA90kYAC9ZX715.jpg" count:3];
sleep(3);
NSLog(@"complete task 3");
dispatch_semaphore_signal(sempone);
});
下载结果如下:
我们可以看到complete task 2 complete task 1 之后 run task 3
第一个第二个下载完成之后执行第三个下载,那么我们信号量控制操作个数的任务就完成了
网友评论