iOS開發多線程-GCD的常見用法

一、延遲執行

1.介紹

iOS常見的延時執行有2種方式

(1)調用NSObject的方法

[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

// 2秒後再調用self的run方法

(2)使用GCD函數

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    // 2秒後異步執行這裏的代碼...

});

2.說明

第一種方法,該方法在那個線程調用,那麼run就在哪個線程執行(當前線程),通常是主線程。
[self performSelector:@selector(run) withObject:nil afterDelay:3.0];
說明:在3秒鐘之後,執行run函數

代碼示例:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"打印當前線程-----%@", [NSThread currentThread]);
    
    // 第一種方法: 延遲2.0秒鐘調用run函數
    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];
}

- (void)run
{
    NSLog(@"%s---延遲執行-----%@", __func__, [NSThread currentThread]);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 在異步函數中執行
    dispatch_queue_t queue = dispatch_queue_create("CoderYLiu", 0);
    
    dispatch_async(queue, ^{
        [self performSelector:@selector(test) withObject:nil afterDelay:1.0];
    });
    
    NSLog(@"異步函數");
}

- (void)test
{
    NSLog(@"%s---異步函數中延遲執行-----%@", __func__, [NSThread currentThread]);
}

@end

說明:如果把該方法放在異步函數中執行,則方法不會被調用(BUG?)

第二種方法:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

       //延遲執行的方法

    });
說明:在5秒鐘之後,執行block中的代碼段。

參數說明:


什麼時間,執行這個隊列中的這個任務。

代碼示例:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"打印當前線程-----%@", [NSThread currentThread]);
    
    // 第二種方式:延遲執行
    // 可以安排其線程(1),主隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"主隊列---延遲執行-----%@", [NSThread currentThread]);
    });
    
    // 可以安排其線程(2),併發隊列
    // 獲取全局併發隊列
    dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 計算任務執行的時間
    dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
    
    // 會在when這個時間點,執行queue2中的這個任務
    dispatch_after(when, queue2, ^{
        NSLog(@"併發隊列---延遲執行-----%@", [NSThread currentThread]);
    });
    
}

@end

延遲執行:不需要再寫方法,且它還傳遞了一個隊列,我們可以指定並安排其線程。

如果隊列是主隊列,那麼就在主線程執行,如果隊列是併發隊列,那麼會新開啓一個線程,在子線程中執行。


二、一次性代碼

1.實現一次性代碼

需求:點擊控制器只有第一次點擊的時候纔打印。

實現代碼:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, assign, getter=isLog) BOOL log;


@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (!self.isLog) {
        NSLog(@"該行代碼只執行一次");
        self.log = YES;
    }
}

@end

   缺點:這是一個對象方法,如果又創建一個新的控制器,那麼打印代碼又會執行,因爲每個新創建的控制器都有自己的布爾類型,且新創建的默認爲NO,因此不能保證該行代碼在整個程序中只打印一次。

2.使用dispatch_once一次性代碼

使用dispatch_once函數能保證某段代碼在程序運行過程中只被執行1次
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

    // 只執行1次的代碼(這裏面默認是線程安全的)

});

整個程序運行過程中,只會執行一次。

代碼示例:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, assign, getter=isLog) BOOL log;


@end

@implementation ViewController

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    if (!self.isLog) {
//        NSLog(@"該行代碼只執行一次");
//        self.log = YES;
//    }
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //一次性代碼:整個程序運行過程中只會執行一次
    /*不能放在懶加載裏面的*/
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 只執行1次的代碼(這裏面默認是線程安全的)
         NSLog(@"該行代碼只執行一次");
    });
}

@end

效果(程序運行過程中,打印代碼只會執行一次):

三、快速迭代

使用dispatch_apply函數能進行快速迭代遍歷

dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
        // 執行10次代碼,index順序不確定
    }

代碼示例1:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 迭代
    for (NSInteger i = 0; i < 10; i++) {
        NSLog(@"%zd-----%@", i, [NSThread currentThread]);
    }
    
    // GCD中的快速迭代
    NSLog(@"----------GCD中的快速迭代----------");
    
    /**
     *
     *  參數1:要遍歷的次數
     *  參數2:隊列(併發)
     *  參數3:size_t 索引
     *
     */
    dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
        NSLog(@"%zd---%@", index, [NSThread currentThread]);
    });
}

@end

執行效果:

需求: 將一個文件夾中的文件剪切到另一個文件夾中

代碼示例:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

/**
 *  需求: 將一個文件夾中的文件剪切到另一個文件夾
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    // 要剪切的文件夾路徑
    NSString *fromPath = @"/Users/Apple/Desktop/from";
    
    // 目標文件夾的路徑
    NSString *toPath = @"/Users/Apple/Desktop/to";
    
    // 得到文件管理者
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    // 得到文件夾中的子路徑
    NSArray *subPaths = [fileManager subpathsAtPath:fromPath];
    
    NSLog(@"%@", subPaths);
    
    // 遍歷文件並執行剪切文件的操作
    NSInteger count = subPaths.count;
    dispatch_apply(count, dispatch_get_global_queue(0, 0), ^(size_t index) {
    
        // 文件的名稱
        NSString *fileName = subPaths[index];
        
        // 拼接文件的全路徑
        NSString *subPath = [fromPath stringByAppendingPathComponent:fileName];
        
        // 拼接剪切的目標路徑
        NSString *fullPath = [toPath stringByAppendingPathComponent:fileName];
        
        // 執行剪切操作
        [fileManager moveItemAtPath:subPath toPath:fullPath error:nil];
        
        NSLog(@"%@--%@,%@",subPath,fullPath,[NSThread currentThread]);
    });
    
}

- (void)MoveFile
{
    // 要剪切的文件夾路徑
    NSString *fromPath = @"/Users/Apple/Desktop/from";
    
    // 目標文件夾的路徑
    NSString *toPath = @"/Users/Apple/Desktop/to";
    
    // 得到文件管理者
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    // 得到文件夾中的子路徑
    NSArray *subPaths = [fileManager subpathsAtPath:fromPath];
    
    NSLog(@"%@", subPaths);
    
    // 遍歷文件並執行剪切文件的操作
    NSInteger count = subPaths.count;
    for (NSInteger i = 0; i < count; i++) {
        // 文件的名稱
        NSString *fileName = subPaths[i];
        
        // 拼接文件的全路徑
        NSString *subPath = [fromPath stringByAppendingPathComponent:fileName];
        
        // 拼接剪切的目標路徑
        NSString *fullPath = [toPath stringByAppendingPathComponent:fileName];
        
        // 執行剪切操作
        [fileManager moveItemAtPath:subPath toPath:fullPath error:nil];
        
        NSLog(@"%@--%@,%@",subPath,fullPath,[NSThread currentThread]);
    }
}

@end

兩種迭代方法的打印效果

for循環:

dispatch_apply:


實際效果可以自己測試

四、隊列組

需求:從網絡上下載兩張圖片,把兩張圖片合併成一張最終顯示在view上。

1.第一種方法

代碼示例:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 獲取全局併發隊列
    dispatch_queue_t globalQuque = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 獲取主隊列
    dispatch_queue_t mainQueue= dispatch_get_main_queue();
    
    dispatch_async(globalQuque, ^{
        // 下載圖片1
        UIImage *image1 = [self imageWithUrl:@"http://img5.hao123.com/data/1_02d75d1d077f83a767fb530ac4a0b80d_510"];
        
        NSLog(@"圖片1下載完成---%@", [NSThread currentThread]);
        
        // 下載圖片2
        UIImage *image2 = [self imageWithUrl:@"http://img1.gamedog.cn/2013/11/12/95-1311120Z3400.jpg"];
        
        NSLog(@"圖片2下載完成---%@", [NSThread currentThread]);
        
        // 回到主線程顯示圖片
        dispatch_async(mainQueue, ^{
            NSLog(@"顯示圖片---%@", [NSThread currentThread]);
            self.imageView1.image = image1;
            self.imageView2.image = image2;
            
            // 合併兩張圖片
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0.0);
            [image1 drawInRect:CGRectMake(0, 0, 150, 300)];
            [image2 drawInRect:CGRectMake(150, 0, 150, 300)];
            self.imageView3.image = UIGraphicsGetImageFromCurrentImageContext();
            // 關閉位圖上下文
            UIGraphicsEndImageContext();
            NSLog(@"圖片合併完成---%@", [NSThread currentThread]);
        });
        
    });

}

// 封裝一個方法,傳人一個url參數,返回一張從網絡資源中下載的圖片
- (UIImage *)imageWithUrl:(NSString *)urlStr
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    return [UIImage imageWithData:data];
}

@end

顯示效果:

打印查看:

問題:這種方式的效率不高,需要等到圖片1.圖片2都下載完成後才行。

提示:使用隊列組可以讓圖片1和圖片2的下載任務同時進行,且當兩個下載任務都完成的時候回到主線程進行顯示。

2.使用隊列組解決

步驟:
  創建一個組
  開啓一個任務下載圖片1
  開啓一個任務下載圖片2
  同時執行下載圖片1\下載圖片2操作
  等group中的所有任務都執行完畢, 再回到主線程執行其他操作

代碼示例
#import "ViewController.h"
// 宏定義全局併發隊列
#define global_quque dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
// 宏定義主隊列
#define main_queue dispatch_get_main_queue()

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 創建一個隊列組
    dispatch_group_t group = dispatch_group_create();
    
    //同時執行下載圖片1和下載圖片2的操作
    // 開啓一個任務下載圖片1
    __block UIImage *image1 = nil;
    dispatch_group_async(group, global_quque, ^{
        image1 = [self imageWithUrl:@"http://img5.hao123.com/data/1_02d75d1d077f83a767fb530ac4a0b80d_510"];
        NSLog(@"圖片1下載完成---%@", [NSThread currentThread]);
    });
    
    // 開啓一個任務下載圖片2
    __block UIImage *image2 = nil;
    dispatch_group_async(group, global_quque, ^{
        image2 = [self imageWithUrl:@"http://img1.gamedog.cn/2013/11/12/95-1311120Z3400.jpg"];
        NSLog(@"圖片2下載完成---%@", [NSThread currentThread]);
    });
    
    //等隊列組group中的所有任務都執行完畢,在回到主線程執行其它操作
    dispatch_group_notify(group, main_queue, ^{
        NSLog(@"顯示圖片---%@", [NSThread currentThread]);
        self.imageView1.image = image1;
        self.imageView2.image = image2;
        
        // 合併兩張圖片
        // 注意最後一個參數是浮點數(0.0),不要寫成0
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0.0);
        [image1 drawInRect:CGRectMake(0, 0, 150, 300)];
        [image2 drawInRect:CGRectMake(150, 0, 150, 300)];
        self.imageView3.image = UIGraphicsGetImageFromCurrentImageContext();
        // 關閉位圖上下文
        UIGraphicsEndImageContext();
        NSLog(@"圖片合併完成---%@", [NSThread currentThread]);
    });
}

@end

打印查看(同時開啓了兩個子線程,分別下載圖片):



2.補充說明

有這麼1種需求:
  首先:分別異步執行2個耗時的操作
  其次:等2個異步操作都執行完畢後,再回到主線程執行操作

如果想要快速高效地實現上述需求,可以考慮用隊列組

dispatch_group_t group =  dispatch_group_create();

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // 執行1個耗時的異步操作

});

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // 執行1個耗時的異步操作

});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{

    // 等前面的異步操作都執行完畢後,回到主線程...

});

五、補充

使用Crearte函數創建的併發隊列和全局併發隊列的主要區別:
1.全局併發隊列在整個應用程序中本身是默認存在的,並且對應有高優先級、默認優先級、低優先級和後臺優先級一共四個併發隊列,我們只是選擇其中的一個直接拿來用。而Crearte函數是實打實的從頭開始去創建一個隊列。
2.在iOS6.0之前,在GCD中凡是使用了帶Crearte和retain的函數在最後都需要做一次release操作。而主隊列和全局併發隊列不需要我們手動release。當然了,在iOS6.0之後GCD已經被納入到了ARC的內存管理範疇中,即便是使用retain或者create函數創建的對象也不再需要開發人員手動釋放,我們像對待普通OC對象一樣對待GCD就OK。
3.在使用柵欄函數的時候,蘋果官方明確規定柵欄函數只有在和使用create函數自己的創建的併發隊列一起使用的時候纔有效(沒有給出具體原因)
4.其它區別涉及到XNU內核的系統級線程編程,不一一列舉。
5.給出一些參考資料(可以自行研究):

GCDAPI:https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_queue_create

Libdispatch版本源碼:http://www.opensource.apple.com/source/libdispatch/libdispatch-187.5/


注意:在iOS9 beta中,蘋果將原http協議改成了https協議,使用 TLS1.2 SSL加密請求數據,所以不能直接使用http協議訪問網絡資源,需要在info.plist 加入key

<key>NSAppTransportSecurity</key>  
<dict>  
<key>NSAllowsArbitraryLoads</key>  
<true/>  
</dict> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章