GCD的常用代码块

一、队列

1、创建一个串行队列

dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_SERIAL);

 

2、创建一个并发队列

dispatch_queue_t queue = dispatch_queue_create("queueName", DISPATCH_QUEUE_CONCURRENT);

 

3、获取主队列

dispatch_queue_t queue = dispatch_get_main_queue();

 

4、获取全局的并发队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

 

 二、执行函数

1、将任务添加到xx队列中去异步执行

dispatch_async(queue, ^{
        NSLog(@"-----下载1---%@", [NSThread currentThread]);
    });

 

 2、将任务添加到xx队列中去同步执行

dispatch_sync(queue, ^{
        NSLog(@"-----下载2---%@", [NSThread currentThread]);
    });

 

 三、线程间的通信

dispatch_async(GlobalQueue, ^{
        // 1.子线程下载图片

        dispatch_async(MainQueue, ^{
                // 2.回到主线程设置图片

        });
    }); 

 

 四、延迟执行

1、3秒后回到主线程执行block中的代码

dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"------task------%@", [NSThread currentThread]);
    });

 

 2、3秒后自动开启新线程执行block中的代码

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"------task------%@", [NSThread currentThread]);
    });

 

 五、一次性执行

+ (instancetype)client {
    static AFNetClient *client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[AFNetClient alloc]init];
    });
    return client;
}

 

六、栅栏函数

dispatch_barrier_async(queue, ^{
        
    });

dispatch_barrier_sync(queue, ^{
        
    });

 

七、快速迭代

NSArray *names = @[@"AAA", @"BBB", @"CCC", @"DDD", @"EEE"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{// dispatch_apply函数对外是同步的,所以在外面套上一层异步多线程
    dispatch_apply(5, queue, ^(size_t index) {
       NSLog(@">>>>>>>>>%@", [names objectAtIndex:index]);
    });
});

 

 八、队列组的使用

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_group_async(group, queue, ^{
  // 1.下载图片1
  });

// 等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程)
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

dispatch_group_async(group, queue, ^{
  // 2.下载图片2
    });
    
// 3.合并图片 (保证执行完组里面的所有任务之后,再执行notify函数里面的block)
dispatch_group_notify(group, queue, ^{
  // 3.合并图片 (保证执行完组里面的所有任务之后,再执行notify函数里面的block)

    dispatch_async(dispatch_get_main_queue(), ^{
    // 4.回到主线程显示图片
        });
});

 

 九、创建单例

1、先创建一个工具宏文件HMSingleton.h,将下面代码全部复制到该头文件中

#endif /* HMSingleton_h */
#define HSFSingletonH(name) + (instancetype)shared##n
#if __has_feature(objc_arc)
#define HSFSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
}
#else
#define HSFSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} \
\
- (oneway void)release { } \
- (id)retain { return self; } \
- (NSUInteger)retainCount { return 1;} \
- (id)autorelease { return self;}
#endif

 

 2、创建单例MHMusicTool

2.1、MHMusicTool.h文件

#import <Foundation/Foundation.h>

@interface HMMusicTool : NSObject
HSFSingletonH(MusicTool)
@end

 

2.2、MHMusicTool.m文件

#import "HMMusicTool.h"

@implementation HMMusicTool
HSFSingletonM(MusicTool)
@end

 

 3、使用单例MHMusicTool

HMMusicTool *tool1 = [HMMusicTool sharedMusicTool];
HMMusicTool *tool2 = [HMMusicTool sharedMusicTool];

 

posted @ 2017-04-20 23:44  码出境界  阅读(367)  评论(1编辑  收藏  举报