一:操作依赖和监听

#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. 【基础练习】【线性DP】codevs2622 数字序列(最大连续子序列和)题解

    版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 这道题目本质就是朴素的最大连续子序列和 直接上题目和代码 题目描写叙述 Descr ...

  2. 细说 iOS 消息推送

    APNS的推送机制 与Android上我们自己实现的推送服务不一样,Apple对设备的控制很严格.消息推送的流程必需要经过APNs: 这里 Provider 是指某个应用的Developer,当然假设 ...

  3. 1.25 Python知识进阶 - 封装

    封装 示例代码: class Role(object): count = 0 def __init__(self,name,role,weapon,life_value=100,money=15000 ...

  4. C#截取指定长度中英文字符串方法 (修改)

    public static string GetFirstString(string stringToSub, int length) { Regex regex = new Regex(" ...

  5. MSSQL相关用法

    一.分页查询 方式一(row_number): SELECT TOP pageSize * FROM (SELECT row_number() OVER (ORDER BY orderColumn) ...

  6. php学习笔记6

    PHP 字符串变量 PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中,我们创建一 ...

  7. 怎样 TabHostFragment自己定义 tab键(indicator)

    1 获得 tabHostFragment: ActionBarActivity activity2 = (ActionBarActivity) activity; mTabHost = new Fra ...

  8. 5lession-path路径相关操作

    今天开始接触到了文件目录.路径方面的知识点.记录如下 先看代码 #!/usr/bin/python # -*- coding: utf-8 -*- import os import sys curre ...

  9. Vue 自定义全局消息框组件

    消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...

  10. Redo 非current损坏

    Redo log 文件损坏或丢失,在启动数据库时在alert日志中会有如下错误: ORA-00313: open failed for members of log group 1 of thread ...