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

一、延遲執行

1.介紹

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

(1)調用NSObject的方法

[objc]view plaincopy

[selfperformSelector:@selector(run)withObject:nilafterDelay:2.0];

//?2秒后再調用self的run方法

(2)使用GCD函數

[objc]view plaincopy

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

//?2秒后異步執行這里的代碼...

});

2.說明

第一種方法,該方法在那個線程調用,那么run就在哪個線程執行(當前線程),通常是主線程。

[objc]view plaincopy

[selfperformSelector:@selector(run)withObject:nilafterDelay:3.0];

說明:在3秒鐘之后,執行run函數

代碼示例:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@end

@implementationViewController

-?(void)viewDidLoad?{

[superviewDidLoad];

NSLog(@"打印當前線程-----%@",?[NSThreadcurrentThread]);

//?第一種方法:?延遲2.0秒鐘調用run函數

[selfperformSelector:@selector(run)withObject:nilafterDelay:2.0];

}

-?(void)run

{

NSLog(@"%s---延遲執行-----%@",?__func__,?[NSThreadcurrentThread]);

}

-?(void)touchesBegan:(NSSet?*)toucheswithEvent:(UIEvent*)event

{

//?在異步函數中執行

dispatch_queue_t?queue?=?dispatch_queue_create("CoderYLiu",0);

dispatch_async(queue,?^{

[selfperformSelector:@selector(test)withObject:nilafterDelay:1.0];

});

NSLog(@"異步函數");

}

-?(void)test

{

NSLog(@"%s---異步函數中延遲執行-----%@",?__func__,?[NSThreadcurrentThread]);

}

@end

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

第二種方法:

[objc]view plaincopy

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

//延遲執行的方法

});

說明:在5秒鐘之后,執行block中的代碼段。

參數說明:

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

代碼示例:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@end

@implementationViewController

-?(void)viewDidLoad?{

[superviewDidLoad];

NSLog(@"打印當前線程-----%@",?[NSThreadcurrentThread]);

//?第二種方式:延遲執行

//?可以安排其線程(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(@"主隊列---延遲執行-----%@",?[NSThreadcurrentThread]);

});

//?可以安排其線程(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(@"并發隊列---延遲執行-----%@",?[NSThreadcurrentThread]);

});

}

@end

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

如果隊列是主隊列,那么就在主線程執行,如果隊列是并發隊列,那么會新開啟一個線程,在子線程中執行。

二、一次性代碼

1.實現一次性代碼

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

實現代碼:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@property(nonatomic,?assign,getter=isLog)BOOLlog;

@end

@implementationViewController

-?(void)touchesBegan:(NSSet?*)toucheswithEvent:(UIEvent*)event

{

if(!self.isLog)?{

NSLog(@"該行代碼只執行一次");

self.log=YES;

}

}

@end

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

2.使用dispatch_once一次性代碼

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

[objc]view plaincopy

staticdispatch_once_t?onceToken;

dispatch_once(&onceToken,?^{

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

});

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

代碼示例:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@property(nonatomic,?assign,getter=isLog)BOOLlog;

@end

@implementationViewController

//-?(void)touchesBegan:(NSSet?*)touches?withEvent:(UIEvent?*)event

//{

//????if?(!self.isLog)?{

//????????NSLog(@"該行代碼只執行一次");

//????????self.log?=?YES;

//????}

//}

-?(void)touchesBegan:(NSSet?*)toucheswithEvent:(UIEvent*)event

{

//一次性代碼:整個程序運行過程中只會執行一次

/*不能放在懶加載里面的*/

staticdispatch_once_t?onceToken;

dispatch_once(&onceToken,?^{

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

NSLog(@"該行代碼只執行一次");

});

}

@end

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

三、快速迭代

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

[objc]view plaincopy

dispatch_apply(10,?dispatch_get_global_queue(0,0),?^(size_tindex)?{

//?執行10次代碼,index順序不確定

}

代碼示例1:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@end

@implementationViewController

-?(void)viewDidLoad?{

[superviewDidLoad];

//?迭代

for(NSInteger?i?=0;?i?<10;?i++)?{

NSLog(@"%zd-----%@",?i,?[NSThreadcurrentThread]);

}

//?GCD中的快速迭代

NSLog(@"----------GCD中的快速迭代----------");

/**

*

*??參數1:要遍歷的次數

*??參數2:隊列(并發)

*??參數3:size_t?索引

*

*/

dispatch_apply(10,?dispatch_get_global_queue(0,0),?^(size_tindex)?{

NSLog(@"%zd---%@",?index,?[NSThreadcurrentThread]);

});

}

@end

執行效果:

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

代碼示例:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@end

@implementationViewController

/**

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

*/

-?(void)viewDidLoad?{

[superviewDidLoad];

//?要剪切的文件夾路徑

NSString*fromPath?=@"/Users/Apple/Desktop/from";

//?目標文件夾的路徑

NSString*toPath?=@"/Users/Apple/Desktop/to";

//?得到文件管理者

NSFileManager*fileManager?=?[NSFileManagerdefaultManager];

//?得到文件夾中的子路徑

NSArray*subPaths?=?[fileManagersubpathsAtPath:fromPath];

NSLog(@"%@",?subPaths);

//?遍歷文件并執行剪切文件的操作

NSInteger?count?=?subPaths.count;

dispatch_apply(count,?dispatch_get_global_queue(0,0),?^(size_tindex)?{

//?文件的名稱

NSString*fileName?=?subPaths[index];

//?拼接文件的全路徑

NSString*subPath?=?[fromPathstringByAppendingPathComponent:fileName];

//?拼接剪切的目標路徑

NSString*fullPath?=?[toPathstringByAppendingPathComponent:fileName];

//?執行剪切操作

[fileManagermoveItemAtPath:subPathtoPath:fullPatherror:nil];

NSLog(@"%@--%@,%@",subPath,fullPath,[NSThreadcurrentThread]);

});

}

-?(void)MoveFile

{

//?要剪切的文件夾路徑

NSString*fromPath?=@"/Users/Apple/Desktop/from";

//?目標文件夾的路徑

NSString*toPath?=@"/Users/Apple/Desktop/to";

//?得到文件管理者

NSFileManager*fileManager?=?[NSFileManagerdefaultManager];

//?得到文件夾中的子路徑

NSArray*subPaths?=?[fileManagersubpathsAtPath:fromPath];

NSLog(@"%@",?subPaths);

//?遍歷文件并執行剪切文件的操作

NSInteger?count?=?subPaths.count;

for(NSInteger?i?=0;?i?<?count;?i++)?{

//?文件的名稱

NSString*fileName?=?subPaths[i];

//?拼接文件的全路徑

NSString*subPath?=?[fromPathstringByAppendingPathComponent:fileName];

//?拼接剪切的目標路徑

NSString*fullPath?=?[toPathstringByAppendingPathComponent:fileName];

//?執行剪切操作

[fileManagermoveItemAtPath:subPathtoPath:fullPatherror:nil];

NSLog(@"%@--%@,%@",subPath,fullPath,[NSThreadcurrentThread]);

}

}

@end

兩種迭代方法的打印效果

for循環:

dispatch_apply:

實際效果可以自己測試

四、隊列組

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

1.第一種方法

代碼示例:

[objc]view plaincopy

#import?"ViewController.h"

@interfaceViewController?()

@property(weak,nonatomic)?IBOutletUIImageView*imageView1;

@property(weak,nonatomic)?IBOutletUIImageView*imageView2;

@property(weak,nonatomic)?IBOutletUIImageView*imageView3;

@end

@implementationViewController

-?(void)touchesBegan:(NSSet?*)toucheswithEvent:(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=?[selfimageWithUrl:@"http://img5.hao123.com/data/1_02d75d1d077f83a767fb530ac4a0b80d_510"];

NSLog(@"圖片1下載完成---%@",?[NSThreadcurrentThread]);

//?下載圖片2

UIImage*image2=?[selfimageWithUrl:@"http://img1.gamedog.cn/2013/11/12/95-1311120Z3400.jpg"];

NSLog(@"圖片2下載完成---%@",?[NSThreadcurrentThread]);

//?回到主線程顯示圖片

dispatch_async(mainQueue,?^{

NSLog(@"顯示圖片---%@",?[NSThreadcurrentThread]);

self.imageView1.image=?image1;

self.imageView2.image=?image2;

//?合并兩張圖片

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300,300),NO,0.0);

[image1drawInRect:CGRectMake(0,0,150,300)];

[image2drawInRect:CGRectMake(150,0,150,300)];

self.imageView3.image=?UIGraphicsGetImageFromCurrentImageContext();

//?關閉位圖上下文

UIGraphicsEndImageContext();

NSLog(@"圖片合并完成---%@",?[NSThreadcurrentThread]);

});

});

}

//?封裝一個方法,傳人一個url參數,返回一張從網絡資源中下載的圖片

-?(UIImage*)imageWithUrl:(NSString*)urlStr

{

NSURL*url?=?[NSURLURLWithString:urlStr];

NSData*data?=?[NSDatadataWithContentsOfURL:url];

return[UIImageimageWithData:data];

}

@end

顯示效果:

打印查看:

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

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

2.使用隊列組解決

步驟:

創建一個組

開啟一個任務下載圖片1

開啟一個任務下載圖片2

同時執行下載圖片1\下載圖片2操作

等group中的所有任務都執行完畢, 再回到主線程執行其他操作

代碼示例

[objc]view plaincopy

#import?"ViewController.h"

//?宏定義全局并發隊列

#define?global_quque?dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,?0)

//?宏定義主隊列

#define?main_queue?dispatch_get_main_queue()

@interfaceViewController?()

@property(weak,nonatomic)?IBOutletUIImageView*imageView1;

@property(weak,nonatomic)?IBOutletUIImageView*imageView2;

@property(weak,nonatomic)?IBOutletUIImageView*imageView3;

@end

@implementationViewController

-?(void)touchesBegan:(NSSet?*)toucheswithEvent:(UIEvent*)event

{

//?創建一個隊列組

dispatch_group_t?group?=?dispatch_group_create();

//同時執行下載圖片1和下載圖片2的操作

//?開啟一個任務下載圖片1

__blockUIImage*image1=nil;

dispatch_group_async(group,?global_quque,?^{

image1=?[selfimageWithUrl:@"http://img5.hao123.com/data/1_02d75d1d077f83a767fb530ac4a0b80d_510"];

NSLog(@"圖片1下載完成---%@",?[NSThreadcurrentThread]);

});

//?開啟一個任務下載圖片2

__blockUIImage*image2=nil;

dispatch_group_async(group,?global_quque,?^{

image2=?[selfimageWithUrl:@"http://img1.gamedog.cn/2013/11/12/95-1311120Z3400.jpg"];

NSLog(@"圖片2下載完成---%@",?[NSThreadcurrentThread]);

});

//等隊列組group中的所有任務都執行完畢,在回到主線程執行其它操作

dispatch_group_notify(group,?main_queue,?^{

NSLog(@"顯示圖片---%@",?[NSThreadcurrentThread]);

self.imageView1.image=?image1;

self.imageView2.image=?image2;

//?合并兩張圖片

//?注意最后一個參數是浮點數(0.0),不要寫成0

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300,300),NO,0.0);

[image1drawInRect:CGRectMake(0,0,150,300)];

[image2drawInRect:CGRectMake(150,0,150,300)];

self.imageView3.image=?UIGraphicsGetImageFromCurrentImageContext();

//?關閉位圖上下文

UIGraphicsEndImageContext();

NSLog(@"圖片合并完成---%@",?[NSThreadcurrentThread]);

});

}

@end

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

2.補充說明

有這么1種需求:

首先:分別異步執行2個耗時的操作

其次:等2個異步操作都執行完畢后,再回到主線程執行操作

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

[objc]view plaincopy

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

[objc]view plaincopy

NSAppTransportSecurity

NSAllowsArbitraryLoads

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容