码迷,mamicode.com
首页 > 其他好文 > 详细

GCD的常用代码块

时间:2017-04-20 23:57:15      阅读:477      评论:0      收藏:0      [点我收藏+]

标签:显示图片   end   self   tac   并发   nsthread   logs   struct   single   

一、队列

1、获取全局的并发队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

2、创建一个串行队列

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

 3、获取主队列

dispatch_queue_t queue = dispatch_get_main_queue();

 二、执行函数

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_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_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

// .h文件
#define HMSingletonH(name) + (instancetype)shared##name;

// .m文件
#if __has_feature(objc_arc)

    #define HMSingletonM(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 HMSingletonM(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
HMSingletonH(MusicTool)
@end

2.2、MHMusicTool.m文件

#import "HMMusicTool.h"

@implementation HMMusicTool
HMSingletonM(MusicTool)
@end

 3、使用单例MHMusicTool

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

 

GCD的常用代码块

标签:显示图片   end   self   tac   并发   nsthread   logs   struct   single   

原文地址:http://www.cnblogs.com/cchHers/p/6741252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!