GCD總結 (三)

來源:互聯網
上載者:User

一、用 GCD 將任務分組

 GCD讓我們建立組,這些組允許你把任務放到一個位置,然後全部運行,運行結束後

會從 GCD 收到一個通知。這一點有很多有價值的用途。例如,假設你有一個 UI-Base APP,想在 UI 上重新載入組件。你有一個表格視圖,一個滾動視圖,一個圖片視圖,就要 用這些方法載入這些組建的內容.

在 GCD 中使用組的時候你應該知道 4 個函數:

dispatch_group_create

建立一個組控制代碼。一旦你使用完了這個組控制代碼,應該使用 dispatch_release 函數將其釋 放。

dispatch_group_async

在一個組內提交一個代碼塊來執行。必須明確這個代碼塊屬於哪個組,必須在哪個派送隊列上執行。

dispatch_group_notify

允許你提交一個 Block Object。一旦添加到這個組的任務完成執行之後,這個 Block Object 應該被執行。這個函數也允許你明確執行 Block Object 的指派隊列。

dispatch_release這個函數釋放那任何一個你通過 dispatch_group_create 函數建立的指派小組。

1、dispatch_group_async

 

//

    dispatch_group_t taskGroup=dispatch_group_create();

    dispatch_queue_t mainQueue1=dispatch_get_main_queue();

    //reload the table view on the main queue

    dispatch_group_async(taskGroup, mainQueue1, ^{

       

        //method 1

        NSLog(@"method1-----");

        

    });

    

    dispatch_group_async(taskGroup, mainQueue1, ^{

        

        //method 2

        NSLog(@"method2----");

    });

    

    //at the end  when we are done,dispatch the following block

    dispatch_group_notify(taskGroup, mainQueue1, ^{

        

        NSLog(@"finished");

        [[[UIAlertView alloc] initWithTitle:@"Finished"

                                    message:@"All tasks are finished" delegate:nil

                          cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

    });

    

    //we are done with the group

    dispatch_release(taskGroup);

2、也可以指派非同步 C 函數到一個指派組來使用 dispatch_group_async_f 函數

 dispatch_group_t taskGroup = dispatch_group_create(); 

dispatch_queue_t mainQueue = dispatch_get_main_queue();

dispatch_group_async_f(taskGroup, mainQueue,

(__bridge void *)self, reloadAllComponents);

/* At the end when we are done, dispatch the following block */ dispatch_group_notify(taskGroup, mainQueue, ^{

/* Do some processing here */[[[UIAlertView alloc] initWithTitle:@"Finished"

});

/* We are done with the group */ 

dispatch_release(taskGroup);

二、用 GCD 構建自己的指派隊列

 利用 GCD,你可以建立你自己的串列指派隊列,串列調度隊列按照先入先出(FIFO)的原則運行它們的任務。然而,串列隊列上的非同步任務不會在主 線程上執行,這就使得串列隊列極需要並發 FIFO 任務。所有提交到一個串列隊列的同步 任務會在當前線程上執行,在任何可能的情況下這個線程會被提交任務的代碼使用。但是提 交到串列隊列的非同步任務總是在主線程以外的線程上執行

 

dispatch_queue_t firstSerialQueue = dispatch_queue_create("com.pixolity.GCD.serialQueue1", 0); dispatch_async(firstSerialQueue, ^{

NSUInteger counter = 0;

for (counter = 0; counter < 5; counter++){

NSLog(@"First iteration, counter = %lu", (unsigned long)counter); }

}); dispatch_async(firstSerialQueue, ^{

NSUInteger counter = 0; for (counter = 0;

counter < 5;

counter++){NSLog(@"Second iteration, counter = %lu", (unsigned long)counter);

} });

dispatch_async(firstSerialQueue, ^{ NSUInteger counter = 0;for (counter = 0;

counter < 5;

counter++){NSLog(@"Third iteration, counter = %lu", (unsigned long)counter);

} });

dispatch_release(firstSerialQueue);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.