GCD简单使用01

本文详细介绍了Grand Central Dispatch (GCD) 的核心概念及其在iOS开发中的使用方法。主要内容包括任务与队列的概念、如何创建串行与并行队列、不同类型的队列在实际开发中的应用案例,以及如何利用GCD进行异步操作和延迟执行。
 

GCD      Grand Central Dispatch  —  — 伟大的中央调度器

 

核心: 将”任务” 放入” 队列”中,确定是同步执行还是异步执行

 

任务: 想要做的事情/ 执行什么操作/执行什么代码.

GCD中任务定义在block中

 

队列(FIFO): 用来存放任务

 

队列 != 线程

队列中的任务最后都要由线程来执行!

 

队列类型:

1> 串行 : Serial Dispatch Queue

one by one  一个任务执行完以后在执行下一个任务,

 

/*

 @param label

 A string label to attach to the queue.

 This parameter is optional and may be NULL.

 @param attr

 DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT, or the result of a call to

 the function dispatch_queue_attr_make_with_qos_class().

 @result

 The newly created dispatch queue.

 */

 

 

    dispatch_queue_t serizlQueue

    = dispatch_queue_create(“这是个串行队列(该队列的描述/附属物)", DISPATCH_QUEUE_SERIAL);

      

    dispatch_queue_t concurrentQueue

       = dispatch_queue_create(“这是个并行队列", DISPATCH_QUEUE_CONCURRENT);

                                                

/*!

 * @function dispatch_get_main_queue

 *

 * @abstract

 * Returns the default queue that is bound to the main thread.

 *

 * @discussion

 * In order to invoke blocks submitted to the main queue, the application must

 * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main

 * thread.

 *

 * @result

 * Returns the main queue. This queue is created automatically on behalf of

 * the main thread before main() is called.

 */

     dispatch_queue_t

     dispatch_get_main_queue(void)

{

    return DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q);

}

                        

获取主队列

dispatch_queue_t mainQueue = dispatch_get_main_queue();

 

获取全局并发队列(后边的参数默认为0)

dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);

 

 

同步,异步

 

同步:  只能 在'当前’线程 中执行任务,不 具备开启新的线程的能力

 

异步: 可以 在新的线程中执行任务, 具备开启新线程的能力

 

实例Code:

 

@interface SAMViewController ()

@property(nonatomic,strong) UIImageView *imageViews;

@property (nonatomic, strong) NSURL *url;

@end

 

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        

        NSData *data = [NSData dataWithContentsOfURL:self.url];

        UIImage *image = [UIImage imageWithData:data];

        dispatch_async(dispatch_get_main_queue(), ^{

            self.imageViews.image = image;

        });

    });

    

}

- (NSURL *)url{

    

    if (_url == nil) {

        NSString *str = @"http://img0.bdstatic.com/img/image/shouye/bizhi0701.jpg";

        str = @"http://127.0.0.1/new/images/angle00.png";

        _url = [NSURL URLWithString:str];

    }

    return _url;

}

- (UIImageView *)imageViews{

    

    if (_imageViews == nil) {

        _imageViews = [[UIImageView alloc] initWithFrame:self.view.bounds];

        [self.view addSubview:_imageViews];

    }

    return _imageViews;

}

 

                        

                        

常见的使用组合:

1>  异步函数 + 全局并发队列   (可以开启多条线程) 

dispatch_async(dispatch_get_global_queue(0, 0), ^{

    // 在这里执行耗时操作代码

    dispatch_async(dispatch_get_main_queue(), ^{

        // 在这里更新UI

    });

});

                        

 

2 >  异步函数 + 串行队列(自定义)   开启一条线程

 

dispatch_queue_t serizl = dispatch_queue_create("serizl", DISPATCH_QUEUE_SERIAL);

dispatch_async(serizl, ^{

    // 执行耗时操作代码 (异步操作)

    dispatch_async(dispatch_get_main_queue(), ^{

        // 执行更新UI操作代码

    });

    

});

                        

 

常见延时执行方式

 

1>

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

    

});

                        

2>

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

 

 

 

队列组的使用:

 

// 队列组的用途

// 执行一组耗时操作执行完后 再回到主线程中执行主线程任务

 

dispatch_group_t group = dispatch_group_create(); // 队列组

dispatch_queue_t queue = dispatch_get_global_queue(0, 0); // 全局并发队列

 

dispatch_group_async(group, queue, ^{         // 异步执行操作1

    // longTime1

});

                        

dispatch_group_async(group, queue, ^{         // 异步执行操作2

    // longTime2

});

                        

dispatch_group_notify(group, dispatch_get_main_queue(), ^{     // 在主线程刷新数据

    // reload Data

});

  

转载于:https://www.cnblogs.com/wolfman4secret/p/4625339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值