iOS開發多線程篇 07 —GCD的基本使用

來源:互聯網
上載者:User

標籤:

iOS開發多線程篇—GCD的基本使用

一、主隊列介紹

主隊列:是和主線程相關聯的隊列,主隊列是GCD內建的一種特殊的串列隊列,放在主隊列中得任務,都會放到主線程中執行。提示:如果把任務放到主隊列中進行處理,那麼不論處理函數是非同步還是同步的都不會開啟新的線程。擷取主隊列的方式:

 dispatch_queue_t queue=dispatch_get_main_queue();

(1)使用非同步函數執行主隊列中得任務,程式碼範例:

 1 // 2 //  YYViewController.m 3 //  12-GCD的基本使用(主隊列) 4 // 5 //  Created by 孔醫己 on 14-6-25. 6 //  Copyright (c) 2014年 itcast. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 13 @end14 15 @implementation YYViewController16 17 - (void)viewDidLoad18 {19     [super viewDidLoad];20     21     //列印主線程22      NSLog(@"列印主線程--%@", [NSThread mainThread]);23     24     //1.擷取主隊列25     dispatch_queue_t queue=dispatch_get_main_queue();26     //2.把任務添加到主隊列中執行27     dispatch_async(queue, ^{28         NSLog(@"使用非同步函數執行主隊列中的任務1--%@",[NSThread currentThread]);29     });30     dispatch_async(queue, ^{31         NSLog(@"使用非同步函數執行主隊列中的任務2--%@",[NSThread currentThread]);32     });33     dispatch_async(queue, ^{34         NSLog(@"使用非同步函數執行主隊列中的任務3--%@",[NSThread currentThread]);35     });36 }37 38 @end

執行效果:

 (2)使用同步函數,在主線程中執行主隊列中得任務,會發生死迴圈,任務無法往下執行。如下:

二、基本使用

1.問題  

任務1和任務2是在主線程執行還是子線程執行,還是單獨再開啟一個新的線程?

 1 // 2 //  YYViewController.m 3 //  13-GCD基本使用(問題) 4 // 5 //  Created by 孔醫己 on 14-6-25. 6 //  Copyright (c) 2014年 itcast. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 13 @end14 15 @implementation YYViewController16 17 - (void)viewDidLoad18 {19     [super viewDidLoad];20     //開啟一個後台線程,調用執行test方法21     [self performSelectorInBackground:@selector(test) withObject:nil];22 }23 24 -(void)test25 {26     NSLog(@"當前線程---%@",[NSThread currentThread]);27     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);28     29     //非同步函數30     dispatch_async(queue, ^{31         NSLog(@"任務1所在的線程----%@",[NSThread currentThread]);32     });33     34     //同步函數35     dispatch_sync(queue, ^{36         NSLog(@"任務2所在的線程----%@",[NSThread currentThread]);37     });38 }39 40 @end

列印結果:

2.開啟子線程,載入圖片

 1 // 2 //  YYViewController.m 3 //  14-GCD基本使用(下載圖片) 4 // 5 //  Created by 孔醫己 on 14-6-25. 6 //  Copyright (c) 2014年 itcast. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 @property (weak, nonatomic) IBOutlet UIImageView *imageView;13 14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     22 }23 24 //當手指觸控螢幕幕的時候,從網路上下載一張圖片到控制器的view上顯示25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event26 {27     28     //1.擷取一個全域串列隊列29     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);30     //2.把任務添加到隊列中執行31     dispatch_async(queue, ^{32         33         //列印當前線程34         NSLog(@"%@",[NSThread currentThread]);35       //3.從網路上下載圖片36         NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];37         NSData *data=[NSData dataWithContentsOfURL:urlstr];38         UIImage *image=[UIImage imageWithData:data];39         //提示40         NSLog(@"圖片載入完畢");41         42         //4.回到主線程,展示圖片43         [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];44     });45 }46 47 @end

顯示效果:

列印結果:

要求使用GCD的方式,在子線程載入圖片完畢後,主線程拿到載入的image重新整理UI介面。

 1 // 2 //  YYViewController.m 3 //  14-GCD基本使用(下載圖片) 4 // 5 //  Created by 孔醫己 on 14-6-25. 6 //  Copyright (c) 2014年 itcast. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 11 @interface YYViewController ()12 @property (weak, nonatomic) IBOutlet UIImageView *imageView;13 14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     22 }23 24 //當手指觸控螢幕幕的時候,從網路上下載一張圖片到控制器的view上顯示25 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event26 {27     28     //1.擷取一個全域串列隊列29     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);30     //2.把任務添加到隊列中執行31     dispatch_async(queue, ^{32         33         //列印當前線程34         NSLog(@"%@",[NSThread currentThread]);35       //3.從網路上下載圖片36         NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];37         NSData *data=[NSData dataWithContentsOfURL:urlstr];38         UIImage *image=[UIImage imageWithData:data];39         //提示40         NSLog(@"圖片載入完畢");41         42         //4.回到主線程,展示圖片43 //        [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];44         dispatch_async(dispatch_get_main_queue(), ^{45             self.imageView.image=image;46             //列印當前線程47             NSLog(@"%@",[NSThread currentThread]);48         });49     });50 }51 52 @end

列印結果:

好處:子線程中得所有資料都可以直接拿到主線程中使用,更加的方便和直觀。

 

三、線程間通訊

從子線程回到主線程

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{// 執?耗時的非同步作業...dispatch_async(dispatch_get_main_queue(), ^{// 回到主線程,執?UI重新整理操作});});  

iOS開發多線程篇 07 —GCD的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.