IOS之GCD基础

IOS之GCD基础

1.栅栏函数的使用

//
//  ViewController.m
//  01-掌握-GCD栅栏函数
//
//  Created by 鲁军 on 2021/6/3.
//
#import "ViewController.h"
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //0.获取全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
        
    });
    dispatch_async(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
    });
    //栅栏函数不能使用全局并发队列
    dispatch_queue_t queue1  = dispatch_queue_create("download", DISPATCH_QUEUE_CONCURRENT);
    //栅栏函数
    dispatch_barrier_async(queue1, ^{
        NSLog(@"++++++++");
    });
    dispatch_async(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
        
    });
}
@end

2.GCD 的快速迭代
文件快速复制案例

//
//  ViewController.m
//  02-掌握-GCD的快速迭代
//
//  Created by 鲁军 on 2021/6/3.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //    [self forDemo];
  // [self apply];
//    [self moveFile];
    [self moveFileWithGCD];
}
-(void)forDemo{
    //同步的执行,主线程
    for(NSInteger i=0;i<10;i++){
        NSLog(@"%zd----%@",i,[NSThread currentThread]);
    }
}
//GCD快速迭代
//开子线程和主线程一起完成遍历任务的,任务的执行是并发的
-(void)apply{
    /**
     遍历的次数
     队列(不可以随便乱传)并发队列。全局的并发队列
     index 索引
     */
    dispatch_apply(10, dispatch_get_global_queue(0, 0) , ^(size_t index) {
        NSLog(@"%zd------%@",index,[NSThread currentThread]);
    });
}
-(void)moveFile{
    //1.拿到文件路径
    NSString *from  = @"/Users/lujun/Desktop/from";
    //2.获得目标文件路径
    NSString *to = @"/Users/lujun/Desktop/to";
    //3.得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    NSLog(@"%@",subPaths);
    //4.遍历所有文件,然后执行剪切操作
    NSInteger count = subPaths.count;
    for(NSInteger i=0;i < count;i++){
        //4.1 拼接文件的全路径
        //NSString *fullPath = [from stringByAppendingString:subPaths[i]];
        //拼接时候会自动添加一个斜杠
        NSString *fullPath = [from stringByAppendingPathComponent:subPaths[i]];
        NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];
        NSLog(@"%@",fullPath);
        //4.2执行文件剪切操作
         BOOL b = [[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toFullPath error:nil];
        NSLog(@"%@,%@,%@",fullPath,toFullPath,[NSThread currentThread]);
    }
}

-(void)moveFileWithGCD{
    //1.拿到文件路径
    NSString *from  = @"/Users/lujun/Desktop/from";
    //2.获得目标文件路径
    NSString *to = @"/Users/lujun/Desktop/to";
    //3.得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    NSLog(@"%@",subPaths);
    //4.遍历所有文件,然后执行剪切操作
    NSInteger count = subPaths.count;
    
    dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t i) {
        //4.1 拼接文件的全路径
        //NSString *fullPath = [from stringByAppendingString:subPaths[i]];
        //拼接时候会自动添加一个斜杠
        NSString *fullPath = [from stringByAppendingPathComponent:subPaths[i]];
        NSString *toFullPath = [to stringByAppendingPathComponent:subPaths[i]];
        NSLog(@"%@",fullPath);
        //4.2执行文件剪切操作
         BOOL b = [[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toFullPath error:nil];
        NSLog(@"%@,%@,%@",fullPath,toFullPath,[NSThread currentThread]);
    });
    

    
}
@end

03- GCD 队列组的基本使用

//
//  ViewController.m
//  03-掌握-GCD队列组的基本使用
//
//  Created by 鲁军 on 2021/6/3.
//
#import "ViewController.h"
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     //1.创建队列
    dispatch_queue_t queue =dispatch_get_global_queue(0, 0);
    
    //2.创建队列组
    dispatch_group_t group = dispatch_group_create();
    
    //3.异步函数
    dispatch_async(queue, ^{
        NSLog(@"1--- %@",[NSThread currentThread]);
    });
    
    //4.生活是什么,按部就班的做好自己的事情
    //4. 生活是什么,就是过日子
    dispatch_async(queue, ^{
        NSLog(@"2--- %@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3--- %@",[NSThread currentThread]);
    });
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值