//延迟方法
// 调用NSObject的方法
// 两秒后再调用self的run的方法
// [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]);
});
}