GCD常用函数

//延迟方法

//    调用NSObject的方法

//    两秒后再调用selfrun的方法

//    [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----");//在主线程中打印

//        dispatch_get_main_queue可以改成并发或串行队列

//    });

   

    

//    NO表示不重复,两秒后,就完事

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

    


}


- (void)run{

    NSLog(@"run---");

}

/**

 *  延迟执行

 */


- (void)delay{

//    整个程序运行中,只会运行一次的操作

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        //onceToken记录有没有执行过这个代码

        NSLog(@"----once");

        

    });

}


//<#size_t iterations#>循环数

//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//并发队列

//   dispatch_apply(10, queue, ^(size_t index) {//同时做10个快速遍历,效率高,

//       

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

//   });


<文件管理>

//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //文件管理者

        //想获取文件夹所有内容      把它转移到另外文件夹中

        //    NSFileManager *mg = [NSFileManager defaultManager];

        //    NSString *from = @"文件拖进来";

        //    NSString *to = @"文件名拖过来";

        //    NSArray *subpaths = [mg subpathsAtPath: from];//有数组接收

        //    for (NSString *subpath in subpaths) {

        //        NSString *formFullpath = [from stringByAppendingPathComponent:subpath];//利用from拼接一个文件名

        //        NSLog(@"%@",formFullpath);

        //        NSString *toFullpath = [to stringByAppendingPathComponent:subpath];

        //        //剪切

        //        [mg moveItemAtPath:formFullpath toPath:toFullpath error:nil];

        //    }

        

  

        

//    });

    

    

//改进

    

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    NSString *from = @"拖进来文件名";

    NSString *to = @"拖进来文件名";

    

    NSFileManager *mgr = [NSFileManager defaultManager];

    NSArray *subpaths = [mgr subpathsAtPath:from];

    

    

    dispatch_apply(subpaths.count, queue, ^(size_t index) {

        NSString *subpath = subpaths[index];

        NSString *fromFullPath = [from stringByAppendingPathComponent:subpath];

        NSString *toFullPath = [to stringByAppendingPathComponent:subpath];

        

        [mgr moveItemAtPath:fromFullPath toPath:toFullPath error:nil];

        

    });

    

    

    

}




#pragma mark - gcd 常用函数

- (void)aa{

//    dispatch_barrier_async(<#dispatch_queue_t queue#>, <#^(void)block#>)

//    在前面的任务执行结束后,它才执行,而且他后面的任务等它执行完成之后才会执行

    

 //   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);不可以

    dispatch_queue_t queue = dispatch_queue_create("dw", DISPATCH_QUEUE_PRIORITY_DEFAULT);

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

    dispatch_async(queue, ^{

        

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

    });

    

    dispatch_async(queue, ^{

        

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

    });

    

    

    

    dispatch_barrier_async(queue, ^{

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

        

    });


    

    dispatch_async(queue, ^{

        

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

    });


    

    dispatch_async(queue, ^{

        

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

    });


    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值