iOS多線程GCD

Grand Central Dispatch (GCD)是Apple開發的一個多核編程的解決方法。編程

dispatch queue分紅如下三種:緩存

1)運行在主線程的Main queue,經過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。app

2)並行隊列global dispatch queue,經過dispatch_get_global_queue獲取,由系統建立三個不一樣優先級的dispatch queue。並行隊列的執行順序與其加入隊列的順序相同。async

3)串行隊列serial queues通常用於按順序同步訪問,可建立任意數量的串行隊列,各個串行隊列之間是併發的。函數

當想要任務按照某一個特定的順序執行時,串行隊列是頗有用的。串行隊列在同一個時間只執行一個任務。咱們可使用串行隊列代替鎖去保護共享的數據。和鎖不一樣,一個串行隊列能夠保證任務在一個可預知的順序下執行。oop

serial queues經過dispatch_queue_create建立,可使用函數dispatch_retain和dispatch_release去增長或者減小引用計數。atom

GCD的用法url

複製代碼

 
 dispatch_async(dispatch_get_global_queue(, ), ^
 dispatch_async(dispatch_get_main_queue(), ^
 &onceToken, ^
  delayInSeconds = = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *^(
 dispatch_queue_t urls_queue = dispatch_queue_create(^
 dispatch_group_t group =,), ^,), ^,), ^
 });

複製代碼

一個應用GCD的例子:spa

複製代碼

    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的另外一個用處是可讓程序在後臺較長久的運行。

在沒有使用GCD時,當app被按home鍵退出後,app僅有最多5秒鐘的時候作一些保存或清理資源的工做。可是在使用GCD後,app最多有10分鐘的時間在後臺長久運行。這個時間能夠用來作清理本地緩存,發送統計數據等工做。

讓程序在後臺長久運行的示例代碼以下:

複製代碼

// AppDelegate.h文件@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;// AppDelegate.m文件- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self beingBackgroundUpdateTask];    // 在這裏加上你須要長久運行的代碼    [self endBackgroundUpdateTask];
}- (void)beingBackgroundUpdateTask
{
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}- (void)endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}

複製代碼

 

ps:

http://www.jianshu.com/p/0b0d9b1f1f19

http://blog.csdn.net/mayday550/article/details/43987365

相關文章
相關標籤/搜索