iOS开发多线程篇—GCD的基本使用
iOS开发多线程篇—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, ); //异步函数
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, );
//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, );
//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, ), ^{
// 执⾏耗时的异步操作...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线程,执⾏UI刷新操作
});
});
iOS开发多线程篇—GCD的基本使用的更多相关文章
- iOS 开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- iOS开发多线程篇—GCD介绍
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- iOS开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- iOS开发多线程篇—GCD简介
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- iOS开发多线程篇 — GCD的常见用法
一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil ...
- iOS开发——多线程篇——GCD
一.基本概念 1.简介什么是GCD全称是Grand Central Dispatch,可译为“牛逼的中枢调度器”纯C语言,提供了非常多强大的函数 GCD的优势GCD是苹果公司为多核的并行运算提出的解决 ...
- iOS开发多线程篇 08 —GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- iOS开发多线程篇 07 —GCD的基本使用
iOS开发多线程篇—GCD的基本使用 一.主队列介绍 主队列:是和主线程相关联的队列,主队列是GCD自带的一种特殊的串行队列,放在主队列中得任务,都会放到主线程中执行. 提示:如果把任务放到主队列中进 ...
- iOS开发多线程篇 05 —GCD介绍
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
随机推荐
- Leetcode: K-th Smallest in Lexicographical Order
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- Android shell命令查询ip,网关,DNS
查看所有网络信息 C:\>adb shell root@android:/ # netcfg netcfg ip6tnl0 DOWN 0.0.0.0/0 0x00000080 00:00:00: ...
- opencv的学习笔记4
通常更加高级的形态学变换,如开闭运算.形态学梯度.“顶帽”.“黑帽”等等,都是可以由常用的腐蚀膨胀技术结合来达到想要的效果. 1.开运算:先腐蚀后膨胀,用于用来消除小物体.在纤细点处分离物体.平滑较大 ...
- innodb数据结构
Jeremy Cole on InnoDB architecture : Efficiently traversing InnoDB B+Trees with the page directory ...
- 关于ClassLoader的一点小问题
今天在看某框架的源码的时候,遇到一个使用URLClassLoader加载Servlet类的问题,当自己在eclipse里面做试验的时候,始终无法把Class加载成功.比如: ClassLoader c ...
- Mac下面的SecureCRT(附破解方案) 更新到最新的8.0.2
继续更新到8.0.2的破解,整体的破解方案都发生了的变化首先还是去 http://macabc.com/detail.htm?app_id=24 下载最新的8.0.2介于很多小白说替换之后说文件损坏, ...
- 关于jstl标签引入的问题
1.源码: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> < ...
- 【20160924】GOCVHelper 图像处理部分(1)
增强后的图像需要通过图像处理获得定量的值.在实际程序设计过程中,轮廓很多时候都是重要的分析变量.参考Halcon的相关函数,我增强了Opencv在这块的相关功能. //寻找最大的轮廓 ...
- JAVA基础知识之NIO——Buffer.Channel,Charset,Channel文件锁
NIO机制 NIO即NEW IO的意思,是JDK1.4提供的针对旧IO体系进行改进之后的IO,新增了许多新类,放在java.nio包下,并对java.io下许多类进行了修改,以便使用与nio. 在ja ...
- oracle分析函数
在工作中使用到的分析函数主要有两种,一个是sum () over (partition by ……order by ……)另外一个就是 lead(lag)over (|partition by|ord ...