GCD总结
GCD Grand Central Dispatch 牛逼的中枢调度器
GCD中各种队列的执行效果
想看线程 必须是异步函数 并且不是主队列
注意:使用sync函数往当前串行队列添加任务,会卡住当前的串行队列
同步函数 立即执行
异步函数 稍后执行(所以不会阻塞)
dispatch_barrier_async(queue, ^{
NSLog(@"-----barrier----%@",[NSThread currentThread]);
});
barrier的作用 在前面的任务执行完再执行 而且他后面的任务等他执行完后才会执行
这里的queue不能是在全局并发队列
必须是自己creat的
GCD快速迭代 同一时间快速遍历
dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
NSLog(@"%zu--%@",index,[NSThread currentThread]);
});
通过GCD创建单例模式
首先从写allocWithZone方法
再创建一个shareInstance 单例类方法
再讲copy方法从写一下
为了便于利用我们可以单独定义一个.h类 创建一个只有宏的类
// .h文件
#define ZMSingletonH(name) + (instancetype)shared##name;
// .m文件
#define ZMSingletonM(name)
static id _instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
+ (instancetype)shared##name
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}