GCD的使用方法

24 篇文章 0 订阅
21 篇文章 0 订阅

在项目开发中 我们用到最多的就是GCD

特总结常用的几点与大家分享 

#import "ViewController.h"

@interface ViewController ()
{
    /**
     *  串行队列
     */
    dispatch_queue_t _mainQueue;
    /**
     *  并行队列
     */
    dispatch_queue_t _globelQueue;
}

@end

@implementation ViewController

/*
 异步调度和同步调度
 异步调度 dispatch_async : 把一个任务添加到某queue后就马上离开,而不管任务在那个queue里的执行状态
 同步调度 dispatch_sync : 把一个任务添加到某queue后,等这个任务完成,调用线程才继续执行.
 
 所以,异步调度和同步调度的区别不在于被添加的任务怎样执行,而在于调用线程是否等待任务执行完
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    串行队列
//    并行队列
    
//GCD:Grand-Central-Dispatch;

    
    
#if 0
    /*
     死锁
     */
    NSLog(@"1111111111");
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"222222222222");
    });
    NSLog(@"33333333333");
    
    只会打印11111111111
#endif
    
    
    _mainQueue = dispatch_get_main_queue();
    _globelQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    [self test4];
    
#if 0
    公式:
    
    dispatch_async(_globelQueue, ^{
        //请求数据
       dispatch_async(_mainQueue, ^{
        //刷新UI
       });
    });
#endif
    
}

//A,B,C表示3个任务,A,B,C并行执行
- (void)test1
{
  
    //A
    dispatch_async(_globelQueue, ^{
        [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"A"];
    });
    //B
    dispatch_async(_globelQueue, ^{
        [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"B"];
    });
    //C
    dispatch_async(_globelQueue, ^{
        [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"C"];
    });
}

//A,B并发执行,最后C
- (void)test2
{
    //调度组
    dispatch_group_t group = dispatch_group_create();
    
    //A
    dispatch_group_async(group, _globelQueue, ^{
        [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"A"];
    });
    
    //B
    dispatch_group_async(group, _globelQueue, ^{
        [self downloadWithUrlString:@"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3" string:@"B"];
    });
    
    //1.其他任务都执行完,最后刷新UI
    //执行的任务 C
    dispatch_group_notify(group, _mainQueue, ^{
        //刷新
    });
    
    //2.有一个下载需要最后执行
    dispatch_group_async(group, _globelQueue, ^{
        
        //执行最后一个任务
        //my code
        
        dispatch_async(_mainQueue, ^{
            //刷新UI
        });
    });
}

//A,B并发执行,然后执行C,最后D,E(一个任务把其他任务分开)
- (void)test3
{
    //barrier需要自定义队列
    /*
     dispatch_queue_create("队列名字", 串行/并行)
     NULL表示DISPATCH_QUEUE_SERIAL
     
     DISPATCH_QUEUE_SERIAL      串行
     DISPATCH_QUEUE_CONCURRENT  并行
    
     */
    
    NSString *urlString = @"http://g.hiphotos.baidu.com/image/h%3D768%3Bcrop%3D0%2C0%2C1024%2C768/sign=ad148bd817ce36d3bd04813602c859f5/42166d224f4a20a4b0824bc592529822720ed010.jpg";
    
   
    dispatch_queue_t customQueue = dispatch_queue_create("com.xxxx.queue", DISPATCH_QUEUE_CONCURRENT);
    
    //A
    dispatch_async(customQueue, ^{
        [self downloadWithUrlString:urlString string:@"A"];
    });
    //B
    dispatch_async(customQueue, ^{
        [self downloadWithUrlString:urlString string:@"B"];
    });
    
    //C
    dispatch_barrier_async(customQueue, ^{
        [self downloadWithUrlString:urlString string:@"C"];
    });
    
    //D
    dispatch_async(customQueue, ^{
        [self downloadWithUrlString:urlString string:@"D"];
    });
    //E
    dispatch_async(customQueue, ^{
        [self downloadWithUrlString:urlString string:@"E"];
    });
}

//执行某个任务多少次
- (void)test4
{
    dispatch_apply(5, _globelQueue, ^(size_t t) {
        NSLog(@"%@",[NSString stringWithFormat:@"%zu",t]);
    });
}

//只执行一次,一般用在单例
- (void)test5
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
    });
}

//延迟调用
- (void)test6
{
    /*
     ull 表示微秒
     */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
    });
}

- (void)downloadWithUrlString:(NSString *)urlString string:(NSString *)string
{
    NSLog(@"开始下载 -- %@",string);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    sleep(2);
    NSLog(@"下载完成 -- %@",string);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值