程序员iOS Developer

栅栏函数与迭代函数

2017-03-29  本文已影响68人  心情的蛊惑

一, 栅栏函数

-(void)barrier {
    // 获取全局并发队列
    // 栅栏函数不能使用全局并发队列dispatch_get_global_queue(0, 0)
//    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_queue_t queue= dispatch_queue_create("dhuui", DISPATCH_QUEUE_CONCURRENT);
    
    //异步函数
    dispatch_async(queue, ^{
      
        for (int i = 0; i <4 ; i++) {
             NSLog(@"download1--%zd---%@---栅栏函数" ,i, [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i <4 ; i++) {
            NSLog(@"download2--%zd---%@---栅栏函数",i , [NSThread currentThread]);
        }
   });
    //栅栏函数
    dispatch_barrier_sync(queue, ^{
        NSLog(@"休息一会");
    });
    dispatch_async(queue, ^{
        for (int i = 0; i <4 ; i++) {
            NSLog(@"download3--%zd---%@---栅栏函数" ,i, [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i <4 ; i++) {
            NSLog(@"download4--%zd---%@---栅栏函数",i , [NSThread currentThread]);
        }
    });
}

二,迭代函数

/*
     参数1,遍历的次数
     参数2,队列(并发队列)
     参数3,index 索引
     */
    dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
        NSLog(@"%zd----%@" ,index , [NSThread currentThread]);
    });
#pragma mark - 利用移动文件,来说明迭代函数
-(void)moveFile {
    //1,拿到文件路径,直接把from拖进来就可以了
    NSString *from = @"/Users/mac/Desktop/GCD/from";
    //2,获得目标文件路径
    NSString *to = @"/Users/mac/Desktop/GCD/to";
    //3,得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    //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]];
        //4.2 剪切
        /*
         参数1,要剪切的文件位置
         参数2,文件剪切到哪
         参数3,
         */
        [[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toFullpath error:nil];
 NSLog(@"%@ ---%@ ---%@" , fullPath , toFullpath , [NSThread currentThread]);
        
    }
    
}
-(void)moveFileWithGCD {
    //1,拿到文件路径,直接把from拖进来就可以了
    NSString *from = @"/Users/mac/Desktop/GCD/from";
    //2,获得目标文件路径
    NSString *to = @"/Users/mac/Desktop/GCD/to";
    //3,得到目录下面的所有文件
    NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:from];
    //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]];
        //4.2 剪切
        /*
         参数1,要剪切的文件位置
         参数2,文件剪切到哪
         参数3,
         */
        [[NSFileManager defaultManager] moveItemAtPath:fullPath toPath:toFullpath error:nil];
        NSLog(@"%@ ---%@ ---%@" , fullPath , toFullpath , [NSThread currentThread]);
    });
    
}
上一篇下一篇

猜你喜欢

热点阅读