4月11日 GCD 总结(一)
 
有 2 種向主隊列分派任務的方法,兩者都是異步的,即使在任務沒有執行的時候也讓你
的程序繼續:
dispatch_async function 在分派隊列上執行一個 Block Object。?
dispatch_async_f function 在分派隊列上執行一個 C 函數。
?
一、dispatch_async function?在分派隊列上執行一個?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?在分派隊列上執行一個?C?函數
 
Dispatch_get_global_queue 函數的第一個參數說明了并發隊列的優先級,這個屬性 GCD
必須替程序員檢索。優先級越高,將提供更多的 CPU Timeslice 來獲取該隊列執行的代碼。
你可以使用下面這些值作為 Dispatch_get_global_queue 函數的第一個參數: DISPATCH_QUEUE_PRIORITY_LOW
您的任務比正常任務用到更少的 Timeslice。
 
DISPATCH_QUEUE_PRIORITY_DEFAULT
執行代碼的默認系統優先級將應用于您的任務。
 
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 的幫助下能夠異步執行 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 总结(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 中国厂商大规模重返 MWC 2023 舞
- 下一篇: 4月11日 GCD 总结(二)
