GCD的常用代码块
一、队列
1、获取全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
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)( * NSEC_PER_SEC)), queue, ^{
NSLog(@"------task------%@", [NSThread currentThread]);
});
2.3秒后自动开启新线程执行block中的代码
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * 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, ); 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 ;} \
- (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的常用代码块的更多相关文章
- vs2015常用代码块与自定义代码块
常用代码块 代码段名 描 述 #if 该代码段用#if和#endif命令围绕代码 #region 该代码段用#region和#endregion命令围绕代码 ~ 该代码段插入一个析构函数 att ...
- 关于Hbuild引用mui常用代码块以及部分控件.
MUI-最接近原生APP体验的高性能前端框架, 追求性能体验,是我们开始启动MUI项目的首要目标,轻量必然是重要特征: MUI不依赖任何第三方JS库,压缩后的JS和CSS文件仅有100+K和60+K, ...
- iOS开发常用代码块(第二弹)
GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispat ...
- iOS开发常用代码块(2)
GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispa ...
- Xcode常用代码块
Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便. 常用的: 1.strong:@property (nonatomic,stron ...
- iOS开发常用代码块
遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...
- iOS 常用代码块
1.判断邮箱格式是否正确的代码: // 利用正则表达式验证 -( BOOL )isValidateEmail:( NSString *)email { NSString *emailRegex ...
- html 常用代码块
解决外边框不计入div尺寸的代码-moz-box-sizing: border-box;box-sizing: border-box;-webkit-box-sizing: border-box; 手 ...
- 常用代码块:创建httpclient 2
HttpGet httpGet = new HttpGet(url);SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(ne ...
随机推荐
- bugku 社工进阶
首先看到的是 由于之前知道有bugku的百度吧 并且这个是一个社工题所以可以试一下这个百度吧 进入百度吧然后会见到 这句话的意思是要我们登录这个账号 但是我们只有账号没有密码 如果爆破的话很有可 ...
- Codeforces Round #599 (Div. 2) C. Tile Painting
Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to ...
- 仿QQ聊天程序(java)
仿QQ聊天程序 转载:牟尼的专栏 http://blog.csdn.net/u012027907 一.设计内容及要求 1.1综述 A.系统概述 我们要做的就是类似QQ这样的面向企业内部的聊天软件,基本 ...
- 【Python】第一个程序---Helloworld!
对于大多数程序语言,第一个入门编程代码便是"Hello World!",以下代码为使用Python输出"Hello World!": #!/usr/bin/py ...
- C# 之 代码实现延时
Task.Delay();异步实现 using System;using System.Threading.Tasks; namespace csharpYS{ class Program { sta ...
- zol验证码抓包
http://www.zol.com/detail/lcd/samsung/25254662.html?zp_from=pro_price_pricenode 网站’ http://city.zol. ...
- window下进程退出后自动重启
设计思想:编写批处理脚本监控进程的运行状态,如果发现进程停止,则自动重启该进程.批处理脚本(jk.bat)和进程脚本(hello.bat)如下: 1.jk.bat @echo off rem 定义需监 ...
- HDU4081 Qin Shi Huang's National Road System
先求最小生成树 再遍历每一对顶点,如果该顶点之间的边属于最小生成树,则剪掉这对顶点在最小生成树里的最长路径 否则直接剪掉连接这对顶点的边~ 用prim算法求最小生成树最长路径的模板~ #include ...
- Spring Boot 编辑器 IDEA 免费许可申请
最近 IDEA 陆续到期(试用版)听说可以申请开源许可,试试吧. 点击 https://www.jetbrains.com/shop/eform/opensource?product=ALL 填写相关 ...
- django之orm的高级操作以及xcc安全攻击
查询用法大全: 1. 比较运算符 # id > 3 res = models.UserInfo.objects.filter(id__gt=3) # id >= 3 res = model ...