一:操作依赖和监听

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
/**
* 1:NSOperation的使用:1:先创建队列NSOperationQueue:若不创建队列直接封装任务则默认在当前线程中串行执行任务,其队列分为两种主队列和非主队列,主队列和GCD中的主队列一样[NSOperationQueue mainQueue],而alloc init 创建出来的队列,默认具有串行和并发的功能,默认是并发的,可以通过指定最大并发数来确定其是串行队列还是主队列,为0,默认什么都不做,为1默认为串行队列,大于1为并发队列 2:封装任务:用NSOperation的子类,NSBlockOperation,NSInvercation,或是继承NSOperation重写main方法封装任务,(还有快速封装任务:1:[queue addOperationWithBlock:<#^(void)block#>]; 2:还可以在某个任务后追加任务:[op4 addExecutionBlock],当某个操作中的任务数量大于1后,会开启子线程,其子线程有可能是开启的异步线程有可能是主线程还有可能是当前线程 3:还可以添加操作监听:op3.completionBlock,当op3任务执行完毕后,就会进入此block执行任务 4:还可以实现操作依赖:可跨队列依赖,但是不能循环依赖addDependency)3:将操作添加到队列中,[queue addOperation:op1];此方法默认执行了[queue start];
*
*/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSOperationQueue *queue2 = [[NSOperationQueue alloc]init]; //2.封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1---%@",[NSThread currentThread]);
}]; NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2---%@",[NSThread currentThread]);
}]; NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3---%@",[NSThread currentThread]);
}]; NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"4---%@",[NSThread currentThread]);
}]; //操作监听
op3.completionBlock = ^{
NSLog(@"++++客官,来看我吧------%@",[NSThread currentThread]);
}; [op4 addExecutionBlock:^{
NSLog(@"5---%@",[NSThread currentThread]);
}]; [op4 addExecutionBlock:^{
NSLog(@"6---%@",[NSThread currentThread]);
}]; [op4 addExecutionBlock:^{
NSLog(@"7---%@",[NSThread currentThread]);
}]; //添加操作依赖
//注意点:不能循环依赖
//可以跨队列依赖
[op1 addDependency:op4];
// [op4 addDependency:op1]; [op2 addDependency:op3]; //添加操作到队列
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue2 addOperation:op4]; } @end

二:线程间通信

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end @implementation ViewController -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self comBie];
} -(void)download
{ //http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e //1.开子线程下载图片
//1.1 非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //1.2 封装操作
NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); //3.更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"UI---%@",[NSThread currentThread]);
}]; }]; //2.添加操作到队列
[queue addOperation:download];
} /*
1.下载图片1
2.下载图片2
3.合并图片
*/
-(void)comBie
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init]; __block UIImage *image1;
__block UIImage *image2;
//2 封装操作,下载图片1
NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
image1 = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); }]; //3 封装操作,下载图片2
NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://www.027art.com/feizhuliu/UploadFiles_6650/201109/2011091718442835.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
image2 = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); }]; //4.封装合并图片的操作
NSBlockOperation *combie = [NSBlockOperation blockOperationWithBlock:^{
//4.1 开上下文
UIGraphicsBeginImageContext(CGSizeMake(, )); //4.2 画图1
[image1 drawInRect:CGRectMake(, , , )]; //4.3 画图2
[image2 drawInRect:CGRectMake(, , , )]; //4.4 根据上下文得到图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //4.5 关闭上下文
UIGraphicsEndImageContext(); //7.更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"UI----%@",[NSThread currentThread]);
}]; }]; //5.设置依赖关系
[combie addDependency:download1];
[combie addDependency:download2]; //6.添加操作到队列中
[queue addOperation:download2];
[queue addOperation:download1];
[queue addOperation:combie];
}
@end

- 2.4 NSOperation实现线程间通信

(1)开子线程下载图片

```objc

//1.创建队列

NSOperationQueue *queue = [[NSOperationQueue alloc]init];

//2.使用简便方法封装操作并添加到队列中

[queue addOperationWithBlock:^{

//3.在该block中下载图片

NSURL *url = [NSURL URLWithString:@"http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];

NSData *data = [NSData dataWithContentsOfURL:url];

UIImage *image = [UIImage imageWithData:data];

NSLog(@"下载图片操作--%@",[NSThread currentThread]);

//4.回到主线程刷新UI

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

self.imageView.image = image;

NSLog(@"刷新UI操作---%@",[NSThread currentThread]);

}];

}];

```

iOS开发NSOperation 三:操作依赖和监听以及线程间通信的更多相关文章

  1. NSOperation操作依赖和监听

    1.操作依赖 NSOperation之间可以设置依赖来保证执行顺序 比如一定要让操作A执行完后,才能执行操作B,可以这么写 [operationB addDependency:operationA]; ...

  2. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  3. XMPPFrameWork IOS 开发(三)登录

    原始地址:XMPPFrameWork IOS 开发(三) XMPP中常用对象们: XMPPStream:xmpp基础服务类 XMPPRoster:好友列表类 XMPPRosterCoreDataSto ...

  4. 线程间通信的三种方式(NSThread,GCD,NSOperation)

    一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...

  5. Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听

    原文:Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听 简单记录下android 盒子开发遥控器的监听 ,希望能帮到新入门的朋友们 不多说,直接贴代码 public cla ...

  6. 【原】iOS多线程之线程间通信和线程互斥

    线程间通信 1> 线程间通信分为两种 主线程进入子线程(前面的方法都可以) 子线程回到主线程 2> 返回主线程 3> 代码 这个案例的思路是:当我触摸屏幕时,会在子线程加载图片,然后 ...

  7. (三)(1)线程间通信---wait和notify的使用

    这篇博客记录线程间通信相关api使用以及理解. 首先第一点,我之前的博客里的线程之间也是通信的,但是他们的通信是建立在访问的是同一个变量上的,相当于是变量.数据层面上的通信,而下面要讲的是线程层面上的 ...

  8. 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题

    调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...

  9. 《JAVA多线程编程核心技术》 笔记:第三章:线程间通信

    一. 等待/通知机制:wait()和notify()1.1.使用的原因:1.2 具体实现:wait()和notify()1.2.1 方法wait():1.2.2 方法notify():1.2.3 wa ...

随机推荐

  1. BZOJ 1507 NOI2003 Editor Splay

    题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...

  2. tokumx的安装和使用

    Add the Tokutek package signing key. $ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key ...

  3. golang pipe

    ===============golang pipe============== package main import ( "fmt" "io" ) func ...

  4. jQ-多选按钮实现单选按钮的功能以及input按钮的优化

    css: .displayN{display: none;} label {font-size:12px;cursor:pointer;} label i {font-size:12px;font-s ...

  5. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  6. 业余学习react 学习记录

    http://www.ruanyifeng.com/blog/2015/03/react (阮一峰 react 学习) 1.搭建环境:npm 使用 React npm install -g cnpm ...

  7. 【习题 6-4 UVA-439】Knight Moves

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs模板题 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sev ...

  8. PatentTips - Use of multiple virtual machine monitors to handle privileged events

    BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VMM) typically runs on a compute ...

  9. Fragment事务管理源代码分析

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/53132952 本文出自: [HansChen的博客] 概述 在Fragment使用中 ...

  10. Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentServiceBean.find(java.lang.String)!

    转自:https://blog.csdn.net/lzx925060109/article/details/40323741 1. Exception in thread "main&quo ...