GCD使用

作者: 那就这样吧_ | 来源:发表于2017-02-21 10:00 被阅读0次

在开发中遇到过这样一个功能,某个界面列表上面是企业,下面是联系人,而且获取企业列表与联系人列表的接口不是同一个,必须等企业跟联系人列表数据都获取完毕后才能刷新,于是就用到了GCD组函数。

1487575235768582.png

然而问题来了,[[IBOSServer shared]...]这玩意儿是封装好的,并且是个异步函数,而异步函数不会阻塞线程,不会等里面的内容执行完,就直接返回了,这就导致数据还没拿到,就开始执行notify里面的代码了。这时需要使用另一种GCD组函数的用法,完美解决问题。

1487575253598331.png

另外:

1、延时执行
- (void)demo{
    // 方法1 timer
    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    // 方法2
    [self performSelector:@selector(task) withObject:nil afterDelay:3];

    // 方法3
    /**
     *参数1:延时时间  dispatch_time生成时间 纳秒为计时单位 精度高
     *参数2:队列
     *参数3:任务
     *异步执行       
     */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        NSLog(@"task");
    });
    NSLog(@"over");
}

- (void)task{
    NSLog(@"task");
}
2、模拟下载任务
/*1、下载 L01.zip
 *2、下载 L02.zip
 *3、通知UI:下载完成
 1、2在3之前执行
 */
- (void)demo1 {
    // 串行,异步顺序下载
    dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(serialQueue, ^{
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒内的随机时间
        dispatch_async(serialQueue, ^{
            NSLog(@"%@下载 L01.zip",[NSThread currentThread]);
        });
        
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒内的随机时间
        dispatch_async(serialQueue, ^{
            NSLog(@"%@下载 L02.zip",[NSThread currentThread]);
        });
        
        dispatch_async(serialQueue, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@下载完成",[NSThread currentThread]);
            });
        });
    });
}
3、并行异步
- (void)demo2 {
    // 并行异步   "shift+command+o"快速查找
    dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(concurrentQueue, ^{
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒内的随机时间
        // 并行同步顺序下载
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"%@下载 L01.zip",[NSThread currentThread]);
        });
        
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒内的随机时间
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"%@下载 L02.zip",[NSThread currentThread]);
        });
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@下载完成",[NSThread currentThread]);
        });
    });
}
4、任务1、2在子线程上先顺序执行后,任务3、4在主线程上执行,最后任务5、6、7在子线程上并发无序执行
- (void)demo3 {
    dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(concurrentQueue, ^{
        dispatch_sync(serialQueue, ^{
            NSLog(@"%@执行任务1",[NSThread currentThread]);
        });
        
        dispatch_sync(serialQueue, ^{
            NSLog(@"%@执行任务2",[NSThread currentThread]);
        });
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"%@执行任务3",[NSThread currentThread]);
        });
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"%@执行任务4",[NSThread currentThread]);
        });
        
        dispatch_async(concurrentQueue, ^{
            dispatch_async(concurrentQueue, ^{
                NSLog(@"%@执行任务5",[NSThread currentThread]);
            });
            dispatch_async(concurrentQueue, ^{
                NSLog(@"%@执行任务6",[NSThread currentThread]);
            });
            dispatch_async(concurrentQueue, ^{
                NSLog(@"%@执行任务7",[NSThread currentThread]);
            });
        });
    });
}
5、队列组
- (void)demo4 {
    NSLog(@"begin");
    // 创建队列组
    dispatch_group_t group =dispatch_group_create();
    
    // 开启异步任务
    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
        // 模拟网络卡
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒内随机时间
        NSLog(@"%@下载 L01.zip",[NSThread currentThread]);
    });
    
    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
        // 模拟网络卡
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒内随机时间
        NSLog(@"%@下载 L02.zip",[NSThread currentThread]);
    });
    
    dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
        NSLog(@"%@下载完成",[NSThread currentThread]);
    });
}
6、GCD组函数的另外使用方式
- (void)demo5 {
    /*    dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)
     {
     dispatch_retain(group);
     dispatch_group_enter(group);
     dispatch_async(queue, ^{
     block();
     dispatch_group_leave(group);
     dispatch_release(group);
     });     */
    
    dispatch_group_t group =dispatch_group_create();
    
    dispatch_group_enter(group);
    // 模拟异步网络请求
    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
        //模拟网络卡
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒内随机时间
        NSLog(@"%@下载 L01.zip",[NSThread currentThread]);
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    // 模拟异步网络请求
    dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
        // 模拟网络卡
        [NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒内随机时间
        NSLog(@"%@下载 L02.zip",[NSThread currentThread]);
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
        NSLog(@"%@下载完成",[NSThread currentThread]);
    });
}

相关文章

  • iOS-多线程:GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(6种不同组合区别) GCD 线程间的通信...

  • iOS多线程--彻底学会多线程之『GCD』

    GCD 文章目录 GCD简介 任务和队列 GCD的使用步骤 队列的创建方法 任务的创建方法 GCD的基本使用 并行...

  • iOS GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(六种组合不同区别,队列嵌套情况区别,相互...

  • 多线程之GCD

    GCD介绍 1、GCD简介 2、GCD任务和队列 3、GCD 的基本使用 4、GCD 线程间的通信 5、GCD 的...

  • iOS 关于GCD很详细的描述

    那为什么我们要使用 GCD 呢? 因为使用 GCD 有很多好处啊,具体如下:GCD 可用于多核的并行运算;GCD ...

  • GCD多线程详解

    1. GCD 简介 2. GCD 任务和队列 3. GCD 的使用步骤 4. GCD 的基本使用(6种不同组合区别...

  • iOS多线程--GCD篇

    GCD 文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步...

  • iOS实录16:GCD使用小结(二)

    iOS实录16:GCD使用小结(二) iOS实录16:GCD使用小结(二)

  • iOS GCD的使用

    什么是GCD了解GCD前,需要了解的基础知识GCD的使用使用注意事项 -GCD学习前铺垫-什么是GCDGCD (G...

  • iOS - GCD

    目录 GCD简介 GCD核心概念 GCD队列的使用 GCD的常见面试题 GCD简介 Grand Central D...

网友评论

      本文标题:GCD使用

      本文链接:https://www.haomeiwen.com/subject/qqmywttx.html