詳解iOS多線(xiàn)程GCD的使用
Grand Central Dispatch(GCD)是異步執(zhí)行任務(wù)的技術(shù)之一
dispatch queue分成以下三種:
1)運(yùn)行在主線(xiàn)程的Main queue,通過(guò)dispatch_get_main_queue獲取。
/*! * @function dispatch_get_main_queue * * @abstract * Returns the default queue that is bound to the main thread. * * @discussion * In order to invoke blocks submitted to the main queue, the application must * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main * thread. * * @result * Returns the main queue. This queue is created automatically on behalf of * the main thread before main() is called. */ __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) DISPATCH_EXPORT struct dispatch_queue_s _dispatch_main_q; #define dispatch_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,通過(guò)dispatch_get_global_queue獲取,由系統(tǒng)創(chuàng)建三個(gè)不同優(yōu)先級(jí)的dispatch queue。并行隊(duì)列的執(zhí)行順序與其加入隊(duì)列的順序相同。
3)串行隊(duì)列serial queues一般用于按順序同步訪問(wèn),可創(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通過(guò)dispatch_queue_create創(chuàng)建,可以使用函數(shù)dispatch_retain和dispatch_release去增加或者減少引用計(jì)數(shù)。
GCD的用法:
// 后臺(tái)執(zhí)行: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // something }); // 主線(xiàn)程執(zhí)行: dispatch_async(dispatch_get_main_queue(), ^{ // something }); // 一次性執(zhí)行: static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // code to be executed once }); // 延遲2秒執(zhí)行: double delayInSeconds = 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_t dispatch_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í)行的線(xiàn)程一 }); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ // 并行執(zhí)行的線(xiàn)程二 }); 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è)用處是可以讓程序在后臺(tái)較長(zhǎng)久的運(yùn)行。
在沒(méi)有使用GCD時(shí),當(dāng)app被按home鍵退出后,app僅有最多5秒鐘的時(shí)候做一些保存或清理資源的工作。但是在使用GCD后,app最多有10分鐘的時(shí)間在后臺(tái)長(zhǎng)久運(yùn)行。這個(gè)時(shí)間可以用來(lái)做清理本地緩存,發(fā)送統(tǒng)計(jì)數(shù)據(jù)等工作。
讓程序在后臺(tái)長(zhǎng)久運(yùn)行的示例代碼如下:
// AppDelegate.h文件 @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; // AppDelegate.m文件 - (void)applicationDidEnterBackground:(UIApplication *)application { [self beingBackgroundUpdateTask]; // 在這里加上你需要長(zhǎng)久運(yùn)行的代碼 [self endBackgroundUpdateTask]; } - (void)beingBackgroundUpdateTask { self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUpdateTask]; }]; } - (void)endBackgroundUpdateTask { [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask]; self.backgroundUpdateTask = UIBackgroundTaskInvalid; }
以上內(nèi)容是小編給大家介紹的IOS中GCD的使用,希望對(duì)大家有所幫助!
相關(guān)文章
iOS簡(jiǎn)單易用的GCD計(jì)時(shí)器的實(shí)現(xiàn)原理
在日常開(kāi)發(fā)中總會(huì)碰到需要計(jì)時(shí)器的功能,常見(jiàn)的定時(shí)器有NSTimer、GCD、CADisplayLink。網(wǎng)上也有很多的教程介紹三者的區(qū)別,今天主要講的是GCD這種方式使用以及封裝。感興趣的小伙伴們可以參考一下2018-11-11iOS中Xcode 8 日志輸出亂碼問(wèn)題的解決方法
這篇文章主要介紹了iOS中Xcode 8日志輸出亂碼問(wèn)題及解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS 9 Core Spotlight搜索實(shí)例代碼
本文主要講解 iOS 9 Core Spotlight,在 IOS 開(kāi)發(fā)的時(shí)候有時(shí)候會(huì)用到搜索功能,這里給大家一個(gè)實(shí)例作為參考,有需要的小伙伴可以參考下2016-07-07iOS 禁止按鈕在一定時(shí)間內(nèi)連續(xù)點(diǎn)擊
本文主要介紹了iOS中禁止按鈕在一定時(shí)間內(nèi)連續(xù)點(diǎn)擊的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02詳解iOS - ASIHTTPRequest 網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了iOS - ASIHTTPRequest 網(wǎng)絡(luò)請(qǐng)求 ,詳細(xì)的介紹了 ASIHTTPRequest的使用,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12android中UIColletionView瀑布流布局實(shí)現(xiàn)思路以及封裝的實(shí)現(xiàn)
本篇文章主要介紹了android中UIColletionView瀑布流布局實(shí)現(xiàn)思路以及封裝的實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02

詳談iOS 位置權(quán)限彈出框閃現(xiàn)的問(wèn)題

iOS 點(diǎn)擊推送消息跳到應(yīng)用指定頁(yè)面的實(shí)例