您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發(61):用 GCD 執行非 UI 相關的任務

編輯:iOS 開發綜合

前言

在實際開發中,我們經常會遇到一些與使用者介面無關的耗時任務,例如大量資料的計算、迴圈處理等。這類工作若放在主執行緒執行,容易造成畫面卡頓。Grand Central Dispatch(GCD)提供了一套以隊列為核心的併發機制,讓我們能輕鬆把任務派送到合適的隊列中執行。

本文將透過範例,說明如何使用 dispatch_sync 在分派隊列上同步執行任務,並比較 Block 與 C 函式兩種寫法。

關鍵概念

dispatch_sync 會把指定的區塊(Block)提交到某個隊列上並同步等待其完成。使用時,你只需取得該隊列的句柄即可。

取得全域併發隊列時會用到 dispatch_get_global_queue,其第一個參數用來指定優先級,GCD 會依此為隊列上的程式碼分配 CPU 時間片;優先級越高,能獲得的執行機會越多。常見的優先級有:

  • DISPATCH_QUEUE_PRIORITY_LOW:任務分配到的時間片比一般任務少。
  • DISPATCH_QUEUE_PRIORITY_DEFAULT:採用系統預設優先級。
  • DISPATCH_QUEUE_PRIORITY_HIGH:任務能獲得比一般任務更多的時間片。

第二個參數目前保留,固定傳入 0 即可。

程式碼實例

TestDemo.h

#import <Foundation/Foundation.h>

@interface TestDemo : NSObject

-(void)testMethod;
-(void)testMethod2;

@end

TestDemo.m

#import "TestDemo.h"

@implementation TestDemo

/************** ObjC Method Start ***************/
// Block 物件
void (^printFrom1To1000)(void) = ^{
    NSUInteger counter = 0;
    for (counter = 1; counter <= 1000; counter++){
        NSLog(@"Counter = %lu - Thread = %@",
              (unsigned long)counter,
              [NSThread currentThread]);
    }
};

// 測試方法:使用 Block 同步執行
-(void)testMethod{
    dispatch_queue_t concurrentQueue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(concurrentQueue, printFrom1To1000);
    dispatch_sync(concurrentQueue, printFrom1To1000);
}
/************** ObjC Method End ***************/

/************** C Method Start ***************/
// C 函式版本
void print2From1To1000(void *paramContext){
    NSUInteger counter = 0;
    for (counter = 1; counter <= 1000; counter++){
        NSLog(@"Counter = %lu - Thread = %@",
              (unsigned long)counter, [NSThread currentThread]);
    }
}

// 測試方法:使用 C 函式同步執行
-(void)testMethod2{
    dispatch_queue_t concurrentQueue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync_f(concurrentQueue, NULL, print2From1To1000);
    dispatch_sync_f(concurrentQueue, NULL, print2From1To1000);
}
/************** C Method End ***************/

@end

main.m

#import <Foundation/Foundation.h>
#import "TestDemo.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        TestDemo *test = [[TestDemo alloc] init];
        // [test testMethod];
        [test testMethod2];
        [test release];
    }
    return 0;
}

執行結果

由於採用同步派送,兩段迴圈會依序執行,且都在同一支執行緒上完成:

2013-05-10 13:50:37.361 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
2013-05-10 13:50:37.363 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
...
2013-05-10 13:50:38.255 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
2013-05-10 13:50:38.256 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}

從輸出可以觀察到,不論使用 Block 還是 C 函式,同步派送都會讓任務依序完成,這正是理解 GCD 隊列行為的良好起點。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved