NSURLSession和多线程网络之GCD简单介绍(任务,队列)

时间:2022-03-16 05:14:56

NSURLConnection是2003年伴随着Safari一起发行的网络开发API,距今已经有十一年。当然,在这十一年间它表现的相当优秀,有大量的应用基础,这也是为什么前面花了那么长时间对它进行详细介绍的原因。但是这些年伴随着iPhone、iPad的发展,对于NSURLConnection设计理念也提出了新的挑战。在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者。相比较NSURLConnection,NSURLSession提供了配置会话缓存、协议、cookie和证书能力,这使得网络架构和应用程序可以独立工作、互不干扰。另外,NSURLSession另一个重要的部分是会话任务,它负责加载数据,在客户端和服务器端进行文件的上传下载。

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

通过前面的介绍大家可以看到,NSURLConnection完成的三个主要任务:获取数据(通常是JSON、XML等)、文件上传、文件下载。其实在NSURLSession时代,他们分别由三个任务来完成:NSURLSessionData、NSURLSessionUploadTask、NSURLSessionDownloadTask,这三个类都是NSURLSessionTask这个抽象类的子类,相比直接使用NSURLConnection,NSURLSessionTask支持任务的暂停、取消和恢复,并且默认任务运行在其他非主线程中,具体关系图如下:

 NSURLSession和多线程网络之GCD简单介绍(任务,队列)

数据请求

前面通过请求一个微博数据进行数据请求演示,现在通过NSURLSessionDataTask实现这个功能,其实现流程与使用NSURLConnection的静态方法类似,下面是主要代码:

-(void)loadJsonData{ //1.创建url NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.208/ViewStatus.aspx?userName=%@&password=%@",@"KenshinCui",@"123"]; urlStr =[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url=[NSURL URLWithString:urlStr]; //2.创建请求 NSURLRequest *request=[NSURLRequest requestWithURL:url]; //3.创建会话(这里使用了一个全局会话)并且启动任务 NSURLSession *session=[NSURLSession sharedSession]; //从会话创建任务 NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (!error) { // NSString *dataStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; // NSLog(@"%@",dataStr);
 
 //主线程中更新UI

dispatch_async(dispatch_get_main_queue(),^{

            // 更新UI

        });

}];
 [dataTask resume];//恢复线程,启动任务}


GCD简单介绍

1.什么是GCD?

全称是Grand Central Dispatch,可译为“牛逼的中枢调度器”

纯C语言,提供了非常多强大的函数

2.GCD的优势

GCD是苹果公司为多核的并行运算提出的解决方案

GCD会自动利用更多的CPU内核(比如双核、四核)

GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程)

程序员只需要告诉GCD想要执行什么任务,不需要编写任何线程管理代码

3.提示

(1)GCD存在于libdispatch.dylib这个库中,这个调度库包含了GCD的所有的东西,但任何IOS程序,默认就加载了这个库,在程序运行的过程中会动态的加载这个库,不需要我们手动导入。

NSURLSession和多线程网络之GCD简单介绍(任务,队列) 
点击+按钮,可以导入框架。 
NSURLSession和多线程网络之GCD简单介绍(任务,队列)

(2)GCD是纯C语言的,因此我们在编写GCD相关代码的时候,面对的函数,而不是方法。 
(3)GCD中的函数大多数都以dispatch开头。

二、任务和队列

GCD中有2个核心概念

(1)任务:执行什么操作

(2)队列:用来存放任务

GCD的使用就2个步骤

(1)定制任务

(2)确定想做的事情

将任务添加到队列中,GCD会自动将队列中的任务取出,放到对应的线程中执行

提示:任务的取出遵循队列的FIFO原则:先进先出,后进后出。

执行任务

1.GCD中有2个用来执行任务的函数

说明:把右边的参数(任务)提交给左边的参数(队列)进行执行。

(1)用同步的方式执行任务 dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);

参数说明:

queue:队列

block:任务

(2)用异步的方式执行任务 dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

2.同步和异步的区别

同步:在当前线程中执行

异步:在另一条线程中执行

队列

1.队列的类型

GCD的队列可以分为2大类型

(1)并发队列(Concurrent Dispatch Queue)

可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务)并发功能只有在异步(dispatch_async)函数下才有效

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

2)串行队列(Serial Dispatch Queue)

让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

2)串行队列(Serial Dispatch Queue)

让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)

3.串行队列

GCD中获得串行有2种途径

(1)使用dispatch_queue_create函数创建串行队列

dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr); // 队列名称, 队列属性,一般用NULL即可

示例:

dispatch_queue_t queue = dispatch_queue_create(“wendingding”, NULL); // 创建

dispatch_release(queue); // 非ARC需要释放手动创建的队列

(2)使用主队列(跟主线程相关联的队列)

主队列是GCD自带的一种特殊的串行队列,放在主队列中的任务,都会放到主线程中执行

使用dispatch_get_main_queue()获得主队列

示例:

dispatch_queue_t queue = dispatch_get_main_queue();

4.并发队列

GCD默认已经提供了全局的并发队列,供整个应用使用,不需要手动创建

使用dispatch_get_global_queue函数获得全局的并发队列

dispatch_queue_t dispatch_get_global_queue(dispatch_queue_priority_t priority,unsigned long flags); // 此参数暂时无用,用0即可

示例:

这个参数是留给以后用的,暂时用不上,传个0。 
第一个参数为优先级,这里选择默认的。获取一个全局的默认优先级的并发队列。

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span> queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得全局并发队列</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

说明:全局并发队列的优先级

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> DISPATCH_QUEUE_PRIORITY_HIGH 2 // 高</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // 默认(中)</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> DISPATCH_QUEUE_PRIORITY_LOW (-2) // 低</span>

<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // 后台</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li></ul>

5.各种队列的执行效果 
NSURLSession和多线程网络之GCD简单介绍(任务,队列)

代码示GCD例任务队列

用异步函数往并发队列中添加任务:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"NYViewController.h"</span></span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NYViewController</span> ()</span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NYViewController</span></span>

- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad
{
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.获得全局的并发队列</span>
   <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span> queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.添加任务到队列中,就可以执行任务</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//异步函数:具备开启新线程的能力</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片1----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//打印主线程</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"主线程----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> mainThread]);

}

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">26</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">27</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">28</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">29</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">30</span></li></ul>

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

总结:同时开启三个子线程

用异步函数往串行队列中添加任务

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad
{
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//打印主线程</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"主线程----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> mainThread]);

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建串行队列</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span>  queue= dispatch_queue_create(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wendingding"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第一个参数为串行队列的名称,是c语言的字符串</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//第二个参数为队列的属性,一般来说串行队列不需要赋值任何属性,所以通常传空值(NULL)</span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.添加任务到队列中执行</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片1----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.释放资源</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//    dispatch_release(queue);</span>
}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">26</span></li></ul>

NSURLSession和多线程网络之GCD简单介绍(任务,队列) 
总结:会开启线程,但是只开启一个线程

用同步函数往并发队列中添加任务

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
 *  用同步函数往并发队列中添加任务
 */</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad
{
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//打印主线程</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"主线程----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> mainThread]);

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建串行队列</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span>  queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);


    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.添加任务到队列中执行</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片1----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片3----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
}
</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">26</span></li></ul>

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

总结:不会开启新的线程,并发队列失去了并发的功能

用同步函数往串行队列中添加任务

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
 *用同步函数往串行队列中添加任务
 */</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad
{
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];
     <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"用同步函数往串行队列中添加任务"</span>);
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//打印主线程</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"主线程----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> mainThread]);

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建串行队列</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span>  queue= dispatch_queue_create(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wendingding"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.添加任务到队列中执行</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片1----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片2----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>(queue, ^{
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"下载图片3----%@"</span>,[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSThread</span> currentThread]);
    });
}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">24</span></li></ul>

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

总结:不会开启新的线程

补充

补充:队列名称的作用:

将来调试的时候,可以看得出任务是在哪个队列中执行的。

NSURLSession和多线程网络之GCD简单介绍(任务,队列)

说明:同步函数不具备开启线程的能力,无论是什么队列都不会开启线程;异步函数具备开启线程的能力,开启几条线程由队列决定(串行队列只会开启一条新的线程,并发队列会开启多条线程)。

同步函数

(1)并发队列:不会开线程

(2)串行队列:不会开线程

异步函数

(1)并发队列:能开启N条线程

(2)串行队列:开启1条线程

补充:

凡是函数中,各种函数名中带有create\copy\new\retain等字眼,都需要在不需要使用这个数据的时候进行release。 
GCD的数据类型在ARC的环境下不需要再做release。 
CF(core Foundation)的数据类型在ARC环境下还是需要做release。 
异步函数具备开线程的能力,但不一定会开线程


GÇD需要掌握的(很重要)

1.队列和任务

1> 任务 :需要执行什么操作 
* 用block来封装任务

2> 队列 :存放任务 
* 全局的并发队列 : 可以让任务并发执行

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-attribute" style="box-sizing: border-box;">dispatch_queue_t queue </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>
  • 自己创建的串行队列 : 让任务一个接着一个执行
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_queue_t</span> queue = dispatch_queue_create(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cn.heima.queue"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>
  • 主队列 : 让任务在主线程执行
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-attribute" style="box-sizing: border-box;">dispatch_queue_t queue </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> dispatch_get_main_queue();</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>

2.执行任务的函数

1> 同步执行 : 不具备开启新线程的能力

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_sync</span>...</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>

2> 异步执行 : 具备开启新线程的能力

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>...</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li></ul>

3.常见的组合(掌握)

1> dispatch_async + 全局并发队列 
2> dispatch_async + 自己创建的串行队列

4.线程间的通信(掌握)

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Comic Sans MS;font-size:18px;">dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), ^{
   // 执行耗时的异步操作<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>


   dispatch_async(dispatch_get_main_queue(), ^{
       // 回到主线程,执行UI刷新操作
   });
});</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Comic Sans MS;font-size:18px;">8</span></li></ul>

5.GCD的所有API都在libdispatch.dylib,Xcode会自动导入这个库

  • 主头文件 : #import

6.延迟执行(掌握)

1> perform…. 
// 3秒后自动回到当前线程调用self的download:方法,并且传递参数:@”http://555.jpg” 
[self performSelector:@selector(download:) withObject:@”http://555.jpg” afterDelay:3];

2> dispatch_after… 
// 任务放到哪个队列中执行 
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
double delay = 3; // 延迟多少秒 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), queue, ^{ 
// 3秒后需要执行的任务 
});

7.一次性代码(掌握)

static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 
// 这里面的代码,在程序运行过程中,永远只会执行1次 
});