iOS开发多线程篇—GCD的基本使用

一、主队列介绍

主队列:是和主线程相关联的队列,主队列是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

执行效果:

(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

打印结果:

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.baidu.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

显示效果:

打印结果:

要求使用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.baidu.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

打印结果:

好处:子线程中得所有数据都可以直接拿到主线程中使用,更加的方便和直观。

三、线程间通信

从子线程回到主线程

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// 执⾏耗时的异步操作...

dispatch_async(dispatch_get_main_queue(), ^{

// 回到主线程,执⾏UI刷新操作

});

});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • NSThread 第一种:通过NSThread的对象方法 NSThread *thread = [[NSThrea...
    攻城狮GG阅读 933评论 0 3
  • #import "ViewController.h" @interface ViewController () @...
    艾克12138阅读 248评论 0 0
  • 知行合一:王阳明 穆斯林的葬礼 一带一路
    KelvinFan阅读 136评论 0 0
  • 啊最近在面暑期实习,海投啊海投希望最后起码能有一个要我-。- 【4.05更】现已拿了一个offer保底,还在争取想...
    BinaryWoodB阅读 3,626评论 9 4
  • 《我的伟人之路》——曾国藩故里 7月24-31日《我的伟人之路》第一期在湖南娄底曾国藩故里举行,来自全国各地的一百...
    柠晨宝宝阅读 198评论 0 0

友情链接更多精彩内容