IOS 多線程GCD詳解
Grand Central Dispatch (GCD)是Apple開發(fā)的一個(gè)多核編程的解決方法。
dispatch queue分成以下三種:
1)運(yùn)行在主線程的Main queue,通過dispatch_get_main_queue獲取。
#definedispatch_get_main_queue() \DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q)
可以看出,dispatch_get_main_queue也是一種dispatch_queue_t。
2)并行隊(duì)列g(shù)lobal dispatch queue,通過dispatch_get_global_queue獲取,由系統(tǒng)創(chuàng)建三個(gè)不同優(yōu)先級的dispatch queue。并行隊(duì)列的執(zhí)行順序與其加入隊(duì)列的順序相同。
3)串行隊(duì)列serial queues一般用于按順序同步訪問,可創(chuàng)建任意數(shù)量的串行隊(duì)列,各個(gè)串行隊(duì)列之間是并發(fā)的。
當(dāng)想要任務(wù)按照某一個(gè)特定的順序執(zhí)行時(shí),串行隊(duì)列是很有用的。串行隊(duì)列在同一個(gè)時(shí)間只執(zhí)行一個(gè)任務(wù)。我們可以使用串行隊(duì)列代替鎖去保護(hù)共享的數(shù)據(jù)。和鎖不同,一個(gè)串行隊(duì)列可以保證任務(wù)在一個(gè)可預(yù)知的順序下執(zhí)行。
serial queues通過dispatch_queue_create創(chuàng)建,可以使用函數(shù)dispatch_retain和dispatch_release去增加或者減少引用計(jì)數(shù)。
GCD的用法:
//后臺執(zhí)行:dispatch_async(dispatch_get_global_queue(0,0), ^{//something}); //主線程執(zhí)行:dispatch_async(dispatch_get_main_queue(), ^{//something}); //一次性執(zhí)行:staticdispatch_once_t onceToken; dispatch_once(&onceToken, ^{//code to be executed once}); //延遲2秒執(zhí)行:doubledelayInSeconds =2.0; dispatch_time_t popTime= dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(),^(void){//code to be executed on the main queue after delay}); //自定義dispatch_queue_tdispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL); dispatch_async(urls_queue,^{//your code}); dispatch_release(urls_queue); //合并匯總結(jié)果dispatch_group_t group =dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{//并行執(zhí)行的線程一}); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{//并行執(zhí)行的線程二}); dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{//匯總結(jié)果});
一個(gè)應(yīng)用GCD的例子:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"]; NSError*error; NSString* data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];if(data !=nil) { dispatch_async(dispatch_get_main_queue(),^{ NSLog(@"call back, the data is: %@", data); }); }else{ NSLog(@"error when download:%@", error); } });
GCD的另一個(gè)用處是可以讓程序在后臺較長久的運(yùn)行。
在沒有使用GCD時(shí),當(dāng)app被按home鍵退出后,app僅有最多5秒鐘的時(shí)候做一些保存或清理資源的工作。但是在使用GCD后,app最多有10分鐘的時(shí)間在后臺長久運(yùn)行。這個(gè)時(shí)間可以用來做清理本地緩存,發(fā)送統(tǒng)計(jì)數(shù)據(jù)等工作。
讓程序在后臺長久運(yùn)行的示例代碼如下:
//AppDelegate.h文件@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;//AppDelegate.m文件- (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBackgroundUpdateTask];//在這里加上你需要長久運(yùn)行的代碼[self endBackgroundUpdateTask]; }- (void)beingBackgroundUpdateTask { self.backgroundUpdateTask= [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUpdateTask]; }]; }- (void)endBackgroundUpdateTask { [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask]; self.backgroundUpdateTask=UIBackgroundTaskInvalid; }
文/牽左手不離(簡書作者)
原文鏈接:http://www.jianshu.com/p/2c584601e3bf
著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。
以上就是對 IOS多線程GCD 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!
- iOS 如何高效的使用多線程
- iOS開發(fā)中多線程的安全隱患總結(jié)
- iOS中的多線程如何按設(shè)定順序去執(zhí)行任務(wù)詳解
- IOS多線程編程N(yùn)SThread的使用方法
- iOS 多線程總結(jié)之GCD的使用詳解
- 詳解iOS 多線程 鎖 互斥 同步
- 詳解iOS多線程GCD問題
- iOS開發(fā)網(wǎng)絡(luò)篇—實(shí)現(xiàn)大文件的多線程斷點(diǎn)下載
- iOS多線程介紹
- iOS多線程開發(fā)——NSThread淺析
- IOS多線程實(shí)現(xiàn)多圖片下載(一)
- IOS多線程實(shí)現(xiàn)多圖片下載(二)
- IOS開發(fā)之多線程N(yùn)SThiread GCD NSOperation Runloop
相關(guān)文章
配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解
這篇文章主要為大家介紹了配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09iOS大文件的分片上傳和斷點(diǎn)上傳的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS大文件的分片上傳和斷點(diǎn)上傳的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-12-12Flutter列表滾動(dòng)定位超強(qiáng)輔助庫使用示例詳解
這篇文章主要為大家介紹了Flutter列表滾動(dòng)定位超強(qiáng)輔助庫使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08iOS實(shí)現(xiàn)支付寶螞蟻森林隨機(jī)按鈕及抖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)支付寶螞蟻森林隨機(jī)按鈕及抖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02iOS 報(bào)clang: error: no input files錯(cuò)誤的解決方法
這篇文章主要給大家介紹了關(guān)于iOS報(bào)clang: error: no input files錯(cuò)誤的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01