日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4月11日 GCD 总结(一)

發(fā)布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4月11日 GCD 总结(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


有 2 種向主隊列分派任務的方法,兩者都是異步的,即使在任務沒有執(zhí)行的時候也讓你

的程序繼續(xù):

dispatch_async function 在分派隊列上執(zhí)行一個 Block Object。?

dispatch_async_f function 在分派隊列上執(zhí)行一個 C 函數。

?

一、dispatch_async function?在分派隊列上執(zhí)行一個?Block Object

Dispatch_sync 方法不能在主隊列中調用,因為無限期的阻止線程并會導致你的應用死 鎖。所有通過 GCD 提交到主隊列的任務必須異步提交。


dispatch_queue_t mainQueue=dispatch_get_main_queue();

? ? dispatch_async(mainQueue, ^{

?? ? ? ?

? ? ? ? [[[UIAlertView alloc] initWithTitle:@"GCD"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:@"GCD is amazing!"

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil cancelButtonTitle:@"OK"

? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:nil, nil] show];

?? ? ? ?

? ? }) ;


二、dispatch_async_f function?在分派隊列上執(zhí)行一個?C?函數


Dispatch_get_global_queue 函數的第一個參數說明了并發(fā)隊列的優(yōu)先級,這個屬性 GCD

必須替程序員檢索。優(yōu)先級越高,將提供更多的 CPU Timeslice 來獲取該隊列執(zhí)行的代碼。

你可以使用下面這些值作為 Dispatch_get_global_queue 函數的第一個參數: DISPATCH_QUEUE_PRIORITY_LOW

您的任務比正常任務用到更少的 Timeslice。


DISPATCH_QUEUE_PRIORITY_DEFAULT

執(zhí)行代碼的默認系統(tǒng)優(yōu)先級將應用于您的任務。


DISPATCH_QUEUE_PRIORITY_HIGH


和正常任務相比,更多的 Timeslices 會應用到你的任務中。

Dispatch_get_global_queue 函數的第二個參數已經保存了,你只要一直給它輸入數值 0就可以了。





dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

? ? size_t numberOfIteration=10;

? ? dispatch_async(queue, ^{

?? ? ? ?

?? ? ? dispatch_apply(numberOfIteration, queue, ^(size_t iteration) {

?? ? ? ? ? //

?? ? ? ? ? NSLog(@"-----dispatch");

?? ? ? });

?? ? ? ?

? ? });



三、


在 GCD 的幫助下能夠異步執(zhí)行 Non-UI 相關任務



dispatch_queue_t currentQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

? ? dispatch_async(currentQueue, ^{

? ? ? ?

? ? ? ? __block UIImage *image=nil;

? ? ? ? dispatch_sync(currentQueue, ^{

? ? ? ? ? ?

? ? ? ? ? ? //download the image here

? ? ? ? ? ? NSString *urlAsString = @"http://images.apple.com/mobileme/features/images/ipad_findyouripad_20100518.jpg";

? ? ? ? ? ? NSURLRequest *urlRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlAsString]];

? ? ? ? ? ? NSError *downloadError=nil;

? ? ? ? ? ? NSData *imageData=[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];

? ? ? ? ? ? if (downloadError == nil && imageData != nil){

? ? ? ? ? ? ? ? image = [UIImage imageWithData:imageData]; /* We have the image. We can use it now */

? ? ? ? ? ? }

? ? ? ? ? ? else if (downloadError != nil)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSLog(@"Error happened = %@", downloadError);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? NSLog(@"No data could get downloaded from the URL.");

? ? ? ? ? ? }

?? ? ? ? ? ?

? ? ? ? });

?? ? ? ?

?? ? ? ?

? ? ? ? dispatch_sync(dispatch_get_main_queue(), ^{

? ? ? ? ? ?

? ? ? ? ? ? //show the image to the user here on the main queue

? ? ? ? ? ? if (image != nil){

? ? ? ? ? ? ? ? /* Create the image view here */

? ? ? ? ? ? ? ? UIImageView *imageView = [[UIImageView alloc]

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithFrame:self.view.bounds];

? ? ? ? ? ? ? ? /* Set the image */ [imageView setImage:image];

? ? ? ? ? ? ? ? /* Make sure the image is not scaled incorrectly */

? ? ? ? ? ? ? ? [imageView setContentMode:UIViewContentModeScaleAspectFit];

? ? ? ? ? ? ? ? /* Add the image to this view controller's view */ [self.view addSubview:imageView];

? ? ? ? ? ? } else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSLog(@"Image isn't downloaded. Nothing to display.");

? ? ? ? ? ? }

?? ? ? ? ? ?

? ? ? ? });

?? ? ? ?

? ? });

總結

以上是生活随笔為你收集整理的4月11日 GCD 总结(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。