你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 詳解iOS中多線程app開辟的GCD隊列的應用

詳解iOS中多線程app開辟的GCD隊列的應用

編輯:IOS開發綜合

GCD的根本應用

1、客隊列引見

客隊列:是和主線程相干聯的隊列,客隊列是GCD自帶的一種特別的串行隊列,放在客隊列中得義務,都邑放到主線程中履行。
提醒:假如把義務放到客隊列中停止處置,那末豈論處置函數是異步的照樣同步的都不會開啟新的線程。
獲得客隊列的方法:


 dispatch_queue_t queue=dispatch_get_main_queue();

(1)應用異步函數履行客隊列中得義務,代碼示例:

//
//  YYViewController.m
//  12-GCD的根本應用(客隊列)
//
//  Created by 孔醫己 on 14-6-25.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //打印主線程
     NSLog(@"打印主線程--%@", [NSThread mainThread]);
    
    //1.獲得客隊列
    dispatch_queue_t queue=dispatch_get_main_queue();
    //2.把義務添加到客隊列中履行
    dispatch_async(queue, ^{
        NSLog(@"應用異步函數履行客隊列中的義務1--%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"應用異步函數履行客隊列中的義務2--%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"應用異步函數履行客隊列中的義務3--%@",[NSThread currentThread]);
    });
}

@end

履行後果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464597.png (889×87)

(2)應用同步函數,在主線程中履行客隊列中得義務,會產生逝世輪回,義務沒法往下履行。表示圖以下:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464526.png (264×225)

2、根本應用

1.成績 

義務1和義務2是在主線程履行照樣子線程履行,照樣零丁再開啟一個新的線程?

//
//  YYViewController.m
//  13-GCD根本應用(成績)
//
//  Created by 孔醫己 on 14-6-25.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //開啟一個後台線程,挪用履行test辦法
    [self performSelectorInBackground:@selector(test) withObject:nil];
}

-(void)test
{
    NSLog(@"以後線程---%@",[NSThread currentThread]);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   
    //異步函數
    dispatch_async(queue, ^{
        NSLog(@"義務1地點的線程----%@",[NSThread currentThread]);
    });
   
    //同步函數
    dispatch_sync(queue, ^{
        NSLog(@"義務2地點的線程----%@",[NSThread currentThread]);
    });
}

@end

打印成果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464565.png (832×62)

2.開啟子線程,加載圖片

//
//  YYViewController.m
//  14-GCD根本應用(下載圖片)
//
//  Created by 孔醫己 on 14-6-25.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
}

//當手指觸摸屏幕的時刻,從收集高低載一張圖片到掌握器的view上顯示
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    //1.獲得一個全局串行隊列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.把義務添加到隊列中履行
    dispatch_async(queue, ^{
       
        //打印以後線程
        NSLog(@"%@",[NSThread currentThread]);
      //3.從收集高低載圖片
        NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.百度.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSData *data=[NSData dataWithContentsOfURL:urlstr];
        UIImage *image=[UIImage imageWithData:data];
        //提醒
        NSLog(@"圖片加載終了");
        
        //4.回到主線程,展現圖片
        [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    });
}

@end

顯示後果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464599.png (348×532)

打印成果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464521.png (816×65)

請求應用GCD的方法,在子線程加載圖片終了後,主線程拿到加載的image刷新UI界面。

//
//  YYViewController.m
//  14-GCD根本應用(下載圖片)
//
//  Created by 孔醫己 on 14-6-25.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
}

//當手指觸摸屏幕的時刻,從收集高低載一張圖片到掌握器的view上顯示
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    //1.獲得一個全局串行隊列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.把義務添加到隊列中履行
    dispatch_async(queue, ^{
       
        //打印以後線程
        NSLog(@"%@",[NSThread currentThread]);
      //3.從收集高低載圖片
        NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.百度.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSData *data=[NSData dataWithContentsOfURL:urlstr];
        UIImage *image=[UIImage imageWithData:data];
        //提醒
        NSLog(@"圖片加載終了");
       
        //4.回到主線程,展現圖片
//        [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image=image;
            //打印以後線程
            NSLog(@"%@",[NSThread currentThread]);
        });
    });
}

@end

打印成果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464563.png (805×75)

利益:子線程中得一切數據都可以直接拿到主線程中應用,加倍的便利和直不雅。

 

3、線程間通訊

從子線程回到主線程

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執⾏耗時的異步操作...
dispatch_async(dispatch_get_main_queue(), ^{

// 回到主線程,執⾏UI刷新操作
});
});


GCD的罕見用法
1、延遲履行
1.引見
IOS罕見的延時履行有2種方法
(1)挪用NSObject的辦法

[self performSelector:@selector(run) withObject:nil afterDelay:2.0];

// 2秒後再挪用self的run辦法

(2)應用GCD函數

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    // 2秒後異步履行這裡的代碼...

});

2.解釋

第一種辦法,該辦法在誰人線程挪用,那末run就在哪一個線程履行(以後線程),平日是主線程。

[self performSelector:@selector(run) withObject:nil afterDelay:3.0];

解釋:在3秒鐘以後,履行run函數

代碼示例:

//
//  YYViewController.m
//  01-GCD的罕見應用(延遲履行)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"打印線程----%@",[NSThread currentThread]);
    //延遲履行
    //第一種辦法:延遲3秒鐘挪用run函數
    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];
   
}
-(void)run
{
    NSLog(@"延遲履行----%@",[NSThread currentThread]);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //在異步函數中履行
    dispatch_queue_t queue = dispatch_queue_create("wendingding", 0);
   
    dispatch_sync(queue, ^{
        [self performSelector:@selector(test) withObject:nil afterDelay:1.0];
    });
    NSLog(@"異步函數");
}
-(void)test
{
    NSLog(@"異步函數中延遲履行----%@",[NSThread currentThread]);
}
@end

解釋:假如把該辦法放在異步函數中履行,則辦法不會被挪用(BUG?)

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464562.png (874×135)

第二種辦法,

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

       //延遲履行的辦法

    });

解釋:在5秒鐘以後,履行block中的代碼段。

參數解釋:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464514.png (728×207)

甚麼時光,履行這個隊列中的這個義務。

代碼示例:

//
//  YYViewController.m
//  02-GCD罕見應用(延遲履行2)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"打印以後線程---%@",  [NSThread currentThread]);
    
    //延遲履行,第二種方法
     //可以支配其線程(1),客隊列
     dispatch_queue_t queue= dispatch_get_main_queue();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"客隊列--延遲履行------%@",[NSThread currentThread]);
    });
   
    //可以支配其線程(2),並發隊列
    //1.獲得全局並發隊列
    dispatch_queue_t queue1= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.盤算義務履行的時光
    dispatch_time_t when=dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
    //3.會在when這個時光點,履行queue中的這個義務
    dispatch_after(when, queue1, ^{
        NSLog(@"並發隊列-延遲履行------%@",[NSThread currentThread]);
    });
}

@end

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464584.png (859×108)

延遲履行:不須要再寫辦法,且它還傳遞了一個隊列,我們可以指定並支配其線程。

假如隊列是客隊列,那末就在主線程履行,假如隊列是並發隊列,那末會新開啟一個線程,在子線程中履行。

 

2、一次性代碼

1.完成一次性代碼

需求:點擊掌握器只要第一次點擊的時刻才打印。

完成代碼:

//
//  YYViewController.m
//  03-GCD罕見應用(一次性代碼)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property(nonatomic,assign) BOOL log;
@end

@implementation YYViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (_log==NO) {
        NSLog(@"該行代碼只履行一次");
        _log=YES;
    }
}
@end

缺陷:這是一個對象辦法,假如又創立一個新的掌握器,那末打印代碼又會履行,由於每一個新創立的掌握器都有本身的布爾類型,且新創立的默許為NO,是以不克不及包管轉業代碼在全部法式中只打印一次。

2.應用dispatch_once一次性代碼

應用dispatch_once函數能包管某段代碼在法式運轉進程中只被履行1次

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

    // 只履行1次的代碼(這外面默許是線程平安的)

});

全部法式運轉進程中,只會履行一次。

代碼示例:

//
//  YYViewController.m
//  03-GCD罕見應用(一次性代碼)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
@property(nonatomic,assign) BOOL log;
@end


@implementation YYViewController

//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    if (_log==NO) {
//        NSLog(@"該行代碼只履行一次");
//        _log=YES;
//    }
//}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"該行代碼只履行一次");
    });
}
@end

後果(法式運轉進程中,打印代碼只會履行一次):

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464547.png (873×96)

3、隊列組

需求:從收集高低載兩張圖片,把兩張圖片歸並成一張終究顯示在view上。

1.第一種辦法

代碼示例:

//
//  YYViewController.m
//  04-GCD根本應用(隊列組下載圖片)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
//宏界說全局並發隊列
#define global_quque    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//宏界說客隊列
#define main_queue       dispatch_get_main_queue()

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //獲得全局並發隊列
//    dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //獲得客隊列
//    dispatch_queue_t queue= dispatch_get_main_queue();
   
//    圖片1:http://d.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg
//    圖片2:http://h.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg
    dispatch_async(global_quque, ^{
        //下載圖片1
       UIImage *image1= [self imageWithUrl:@"http://d.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"];
        NSLog(@"圖片1下載完成---%@",[NSThread currentThread]);
   
        //下載圖片2
       UIImage *image2= [self imageWithUrl:@"http://h.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSLog(@"圖片2下載完成---%@",[NSThread currentThread]);
       
        //回到主線程顯示圖片
        dispatch_async(main_queue, ^{
             NSLog(@"顯示圖片---%@",[NSThread currentThread]);
            self.imageView1.image=image1;
            self.imageView2.image=image2;
            //歸並兩張圖片
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
            [image1 draWinRect:CGRectMake(0, 0, 100, 100)];
            [image2 draWinRect:CGRectMake(100, 0, 100, 100)];
            self.imageView3.image=UIGraphicsGetImageFromCurrentImageContext();
            //封閉高低文
            UIGraphicsEndImageContext();
               NSLog(@"圖片歸並完成---%@",[NSThread currentThread]);
        });
        //
    });
}

//封裝一個辦法,傳入一個url參數,前往一張收集高低載的圖片
-(UIImage *)imageWithUrl:(NSString *)urlStr
{
    NSURL *url=[NSURL URLWithString:urlStr];
    NSData *data=[NSData dataWithContentsOfURL:url];
    UIImage *image=[UIImage imageWithData:data];
    return image;
}
@end

顯示後果:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464685.png (348×532)

打印檢查:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464655.png (845×101)

成績:這類方法的效力不高,須要比及圖片1.圖片2都下載完成後才行。

提醒:應用隊列組可讓圖片1和圖片2的下載義務同時停止,且當兩個下載義務都完成的時刻回到主線程停止顯示。

2.應用隊列組處理

步調:

創立一個組

開啟一個義務下載圖片1

 開啟一個義務下載圖片2

同時履行下載圖片1\下載圖片2操作

等group中的一切義務都履行終了, 再回到主線程履行其他操作

代碼示例

//
//  YYViewController.m
//  04-GCD根本應用(隊列組下載圖片)
//
//  Created by apple on 14-6-25.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
//宏界說全局並發隊列
#define global_quque    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//宏界說客隊列
#define main_queue       dispatch_get_main_queue()

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //    圖片1:http://d.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg
    //    圖片2:http://h.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg
   
   
    //1.創立一個隊列組
        dispatch_group_t group = dispatch_group_create();
    
    //2.開啟一個義務下載圖片1
    __block UIImage *image1=nil;
    dispatch_group_async(group, global_quque, ^{
        image1= [self imageWithUrl:@"http://d.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"];
        NSLog(@"圖片1下載完成---%@",[NSThread currentThread]);
    });
   
    //3.開啟一個義務下載圖片2
    __block UIImage *image2=nil;
    dispatch_group_async(group, global_quque, ^{
        image2= [self imageWithUrl:@"http://h.hiphotos.百度.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
        NSLog(@"圖片2下載完成---%@",[NSThread currentThread]);
    });
   
    //同時履行下載圖片1\下載圖片2操作
   
   //4.等group中的一切義務都履行終了, 再回到主線程履行其他操作
    dispatch_group_notify(group,main_queue, ^{
        NSLog(@"顯示圖片---%@",[NSThread currentThread]);
        self.imageView1.image=image1;
        self.imageView2.image=image2;
       
        //歸並兩張圖片
        //留意最初一個參數是浮點數(0.0),不要寫成0。
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
        [image1 draWinRect:CGRectMake(0, 0, 100, 100)];
        [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
        self.imageView3.image=UIGraphicsGetImageFromCurrentImageContext();
        //封閉高低文
        UIGraphicsEndImageContext();
       
        NSLog(@"圖片歸並完成---%@",[NSThread currentThread]);
    });
   
}
-(void)download2image
{
    //獲得全局並發隊列
//    dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //獲得客隊列
//    dispatch_queue_t queue= dispatch_get_main_queue();
   
    dispatch_async(global_quque, ^{
        //下載圖片1
       UIImage *image1= [self imageWithUrl:@"http://news.百度.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
        NSLog(@"圖片1下載完成---%@",[NSThread currentThread]);
   
        //下載圖片2
       UIImage *image2= [self imageWithUrl:@"http://news.百度.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
        NSLog(@"圖片2下載完成---%@",[NSThread currentThread]);
       
        //回到主線程顯示圖片
        dispatch_async(main_queue, ^{
             NSLog(@"顯示圖片---%@",[NSThread currentThread]);
            self.imageView1.image=image1;
            self.imageView2.image=image2;
            //歸並兩張圖片
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
            [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
            [image2 drawInRect:CGRectMake(0, 0, 100, 100)];
            self.imageView3.image=UIGraphicsGetImageFromCurrentImageContext();
            //封閉高低文
            UIGraphicsEndImageContext();
               NSLog(@"圖片歸並完成---%@",[NSThread currentThread]);
        });
        //
    });
}

//封裝一個辦法,傳入一個url參數,前往一張收集高低載的圖片
-(UIImage *)imageWithUrl:(NSString *)urlStr
{
    NSURL *url=[NSURL URLWithString:urlStr];
    NSData *data=[NSData dataWithContentsOfURL:url];
    UIImage *image=[UIImage imageWithData:data];
    return image;
}
@end

打印檢查(同時開啟了兩個子線程,分離下載圖片):

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615464673.png (837×92)

2.彌補解釋

有這麼1種需求:

起首:分離異步履行2個耗時的操作

其次:等2個異步操作都履行終了後,再回到主線程履行操作

 

假如想要疾速高效地完成上述需求,可以斟酌用隊列組

dispatch_group_t group =  dispatch_group_create();

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // 履行1個耗時的異步操作

});

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // 履行1個耗時的異步操作

});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{

    // 等後面的異步操作都履行終了後,回到主線程...

});

【詳解iOS中多線程app開辟的GCD隊列的應用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved