iOS GCD使用

記錄我工作中使用到的GCD方法,定期更新完善。
Grand Central Dispatch (GCD) 是Apple開發(fā)的一個(gè)多核編程的較新的解決方法。

  1. GCD可用于多核的并行運(yùn)算
  2. GCD會自動利用更多的CPU內(nèi)核(比如雙核、四核)
  3. GCD會自動管理線程的生命周期(創(chuàng)建線程、調(diào)度任務(wù)、銷毀線程)

程序員只需要告訴GCD想要執(zhí)行什么任務(wù),不需要編寫任何線程管理代碼。但是看了很多文章,感覺GCD太大了,根本就記不住,所以寫下這個(gè)筆記,記錄下自己使用到的方法。

  1. 同時(shí)執(zhí)行多個(gè)異步操作,都執(zhí)行完畢,再執(zhí)行某個(gè)方法。
    // 創(chuàng)建信號量
    _semaphore = dispatch_semaphore_create(0);
   // 創(chuàng)建全局并行
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        // 請求一
        //獲取輪播圖數(shù)據(jù)
        [self getCarouselData];
    });
    dispatch_group_async(group, queue, ^{
        // 請求二
        //獲取首頁 3 和 8 張圖
        [self getTemplateImg];
    });
    dispatch_group_notify(group, queue, ^{
        // 2個(gè)請求對應(yīng)2次信號等待
        dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
        //回到主線程刷新ui
        dispatch_async(dispatch_get_main_queue(), ^{
            DLog(@"------------  請求完畢 ---------------");

            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        });
    });

在請求方法獲取到數(shù)據(jù)時(shí)

   //[self getCarouselData];
   DLog(@"------------ 第一次 請求完畢 ---------------");
   dispatch_semaphore_signal(_semaphore);
  1. GCD進(jìn)行延遲操作
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        
        [self.navigationController popViewControllerAnimated:YES];
    });
  1. GCD倒計(jì)時(shí)
    __block int timeout= 59; //倒計(jì)時(shí)時(shí)間
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒執(zhí)行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒計(jì)時(shí)結(jié)束,關(guān)閉
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置
                [self.getCode setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal];
                self.getCode.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 120;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置
                [self.getCode setTitle:[NSString stringWithFormat:@"%@秒后重獲",strTime] forState:UIControlStateNormal];
                self.getCode.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);

4.異步操作,主線程刷新UI

            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                //通知主線程刷新
                dispatch_async(dispatch_get_main_queue(), ^{
                    //回調(diào)或者說是通知主線程刷新,
                    [self.mTableView reloadData];;
                });
            });

5.調(diào)度組

//1.創(chuàng)建調(diào)度組
    dispatch_group_t group = dispatch_group_create();
    //2.隊(duì)列
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    //3.調(diào)度組監(jiān)聽隊(duì)列
    dispatch_group_enter(group);
    dispatch_group_async(group, q, ^{
        sleep(1);
        NSLog(@"download 1");
        dispatch_group_leave(group);
    });
    dispatch_group_enter(group);
    dispatch_group_async(group, q, ^{
        NSLog(@"download 2");
        dispatch_group_leave(group);
    });
    dispatch_group_notify(group, q, ^{
        NSLog(@"完成");
    });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容