ios GCD一些使用方法

來源:互聯網
上載者:User

標籤:

 1 - (void)GCDMethed{ 2     // 後台執行 3     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 4          5         // 後台執行的操作 6         NSLog(@"後台操作"); 7     }); 8      9     // 主線程執行10     dispatch_async(dispatch_get_main_queue(), ^{11        12         // 主線程執行的操作13         NSLog(@"主線程操作");14     });15     16     17     // 一次性執行18     for (int i=0; i<2; i++) {19         static  dispatch_once_t onceToken;20         dispatch_once(&onceToken, ^{21             // 代碼執行一次22             NSLog(@"代碼執行一次");23         });24     }25     26 //    延遲2秒執行27     double delayInSeconds = 2.0;28     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));29     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){30         NSLog(@"延遲了2秒執行");31     });32     33     // 自訂 dispatch_queue_t34     dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);35     dispatch_queue_t urls_queue2 = dispatch_queue_create("TEST",DISPATCH_QUEUE_CONCURRENT); // 並行的隊列 DISPATCH_QUEUE_SERIAL 穿行隊列36     dispatch_async(urls_queue, ^{37         38     });39 //    dispatch_release(urls_queue);  ARC 不需要40     41     42     // 合并匯總結果43     44     dispatch_group_t group  = dispatch_group_create();45     dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{46             // 並存執行的線程一47     });48     dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{49             // 並存執行的線程二50     });51     dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{52             // 匯總結果53     });54     55     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{56        57         dispatch_async(dispatch_get_main_queue(), ^{58             59         });60     });61     62 }

 

ios GCD一些使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.