iOS開發之GCD使用,ios開發gcd

來源:互聯網
上載者:User

iOS開發之GCD使用,ios開發gcd
iOS開發之GCD使用(1)使用GCD建立一個非同步任務

#pragma mark - 使用GCD建立一個非同步任務- (void)createAsyncTask{    //建立一個非同步任務    //參數1:傳入queue,有3種queue    //main queue  主隊列(UI主線程)    //global queue 全域隊列(理解為背景工作執行緒)    //自訂queue    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(queue, ^{        for (int i=0; i<20; i++) {            NSLog(@"A = %d",i);        }    });    dispatch_async(queue, ^{        for (int i=0; i<20; i++) {            NSLog(@"B = %d",i);        }    });    }

 

(2)類比網路下載
#pragma mark - 類比網路下載- (void)simulateNetwordDownload{    _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 300, 20)];    [self.view addSubview:_progressView];        //GCD最簡單開啟非同步任務的形式    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        for (int i=0; i<100; i++) {            //子線程中不能直接更新UI//            progressView.progress+=0.01;            //(1)更新UI  dispatch_get_main_queue()  作用:切回主線程更新UI            dispatch_async(dispatch_get_main_queue(), ^{                    _progressView.progress+=0.01;            });                        [NSThread sleepForTimeInterval:0.1];        }                //(2)最後顯示對話方塊        dispatch_async(dispatch_get_main_queue(), ^{            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下載完成" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];            [alert show];        });        //(1)(2)同步執行,先(1)後(2)    });        }

 

(3)只執行一次,實現單例(推薦實現的方式,安全執行緒)
#pragma mark - 只執行一次,實現單例(推薦實現的方式,安全執行緒)- (void)runOnce{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"只執行一次的代碼");    });}

 

(4)延時執行
#pragma mark - 延時執行- (void)delayRun{        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"1111111");    });}

 

(5)通知同時非同步執行多個任務,等待所有任務下載完成進行處理(類似迅雷)
#pragma mark - 通知執行多個任務,等待所有任務下載完成進行處理(類似迅雷)- (void)groupRun{    //group 工作群組    dispatch_group_t group = dispatch_group_create();        //7s完成    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        for (int i=0; i<100; i++) {            NSLog(@"A = %d",i);            [NSThread sleepForTimeInterval:0.07];        }    });    //5s完成    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        for (int i=0; i<100; i++) {            NSLog(@"B = %d",i);            [NSThread sleepForTimeInterval:0.05];        }    });    //10s完成    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        for (int i=0; i<100; i++) {            NSLog(@"C = %d",i);            [NSThread sleepForTimeInterval:0.1];        }    });        dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        NSLog(@"所有任務完成,自動關機");    });}

 

相關文章

聯繫我們

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