iOS多线程---NSOperation的常用操作
1.最大并发数:
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = ;
(1)取消队列的所有操作
- (void)cancelAllOperations;
提⽰:也可以调用NSOperation的- (void)cancel⽅法取消单个操作
(2)暂停和恢复队列
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列
- (BOOL)isSuspended; //当前状态
3.操作优先级 : 设置NSOPeration的优先级,可以改变在NSOPerationQueue中的执行优先级
[operation2 setQueuePriority:NSOperationQueuePriorityNormal]; //设置优先级
NSOperationQueuePriority priority = operation2.queuePriority; //获去操作的优先级
4.操作依赖
有两个操作,operationA:买饭和operationB:吃饭,必须要先买饭,然后再吃饭。操作B:吃饭 必须等到操作A:买饭 的操作完成后才能执行。这就是依赖关系。
在NSOPeration中描述依赖用:[operationB addDependency:operationA];
- (void)viewDidLoad {
[super viewDidLoad];
NSInvocationOperation *operationA = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object:nil];
NSInvocationOperation *operationB = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction1) object:nil];
[operationB addDependency:operationA];
//定义队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//将操作添加到队列
[queue addOperation:operationA];
[queue addOperation:operationB];
}
-(void)testAction
{
for (int i = ; i < ; i ++) {
NSLog(@"给五个人买饭带回宿舍----%@",[NSThread currentThread]);
}
}
-(void)testAction1
{
for (int i = ; i < ; i ++) {
NSLog(@"五个人开始吃饭----%@",[NSThread currentThread]);
}
}
2017-06-18 11:33:26.765 demo[17639:3076261] 给五个人买饭带回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.766 demo[17639:3076261] 给五个人买饭带回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.767 demo[17639:3076261] 给五个人买饭带回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.768 demo[17639:3076261] 给五个人买饭带回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.769 demo[17639:3076261] 给五个人买饭带回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.770 demo[17639:3076261] 五个人开始吃饭----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.776 demo[17639:3076261] 五个人开始吃饭----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.777 demo[17639:3076261] 五个人开始吃饭----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.781 demo[17639:3076261] 五个人开始吃饭----<NSThread: 0x600000267b40>{number = 3, name = (null)}
2017-06-18 11:33:26.782 demo[17639:3076261] 五个人开始吃饭----<NSThread: 0x600000267b40>{number = 3, name = (null)}
注:用for循环更能看出操作的执行顺序。
5.操作的监听
有时候需要执行完一个操作再执行另个操作,可以将这两个操作写在一起,但是当代码很多的时候,会造成阅读性不强。
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i < ; i ++) {
NSLog(@"给五个人买饭带回宿舍----%@",[NSThread currentThread]);
}
for (int i = ; i < ; i ++) {
NSLog(@"五个人开始吃饭----%@",[NSThread currentThread]);
}
}];
//定义队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//将操作添加到队列
[queue addOperation:operation];
也可以分开写:
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i < ; i ++) {
NSLog(@"给五个人买饭带回宿舍----%@",[NSThread currentThread]);
}
}];
operation.completionBlock = ^{
for (int i = ; i < ; i ++) {
NSLog(@"五个人开始吃饭----%@",[NSThread currentThread]);
}
};
//定义队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//将操作添加到队列
[queue addOperation:operation];
2017-06-18 11:41:21.883 demo[17675:3084893] 给五个人买饭带回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}
2017-06-18 11:41:21.885 demo[17675:3084893] 给五个人买饭带回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}
2017-06-18 11:41:21.886 demo[17675:3084893] 给五个人买饭带回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}
2017-06-18 11:41:21.888 demo[17675:3084893] 给五个人买饭带回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}
2017-06-18 11:41:21.888 demo[17675:3084893] 给五个人买饭带回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}
2017-06-18 11:41:21.894 demo[17675:3084895] 五个人开始吃饭----<NSThread: 0x60800006c600>{number = 4, name = (null)}
2017-06-18 11:41:21.895 demo[17675:3084895] 五个人开始吃饭----<NSThread: 0x60800006c600>{number = 4, name = (null)}
2017-06-18 11:41:21.896 demo[17675:3084895] 五个人开始吃饭----<NSThread: 0x60800006c600>{number = 4, name = (null)}
2017-06-18 11:41:21.897 demo[17675:3084895] 五个人开始吃饭----<NSThread: 0x60800006c600>{number = 4, name = (null)}
2017-06-18 11:41:21.898 demo[17675:3084895] 五个人开始吃饭----<NSThread: 0x60800006c600>{number = 4, name = (null)}
注:可以看出当买饭的操作完成后,开启一个新线程执行吃饭操作。
iOS多线程---NSOperation的常用操作的更多相关文章
- iOS 多线程 NSOperation、NSOperationQueue
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...
- iOS开发:UINavigationController常用操作
NavigationController常用操作: 更改bar的背景颜色:self.navigationController?.navigationBar.barTintColor =UIColor. ...
- iOS多线程 NSOperation的用法
上一篇写了 GCD 的使用,接下来就了解一下 NSOperation ,NSOperation是苹果对 GCD 的 OC 版的一个封装,但是相对于GCD来说可控性更强,并且可以加入操作依赖. NSOp ...
- ios多线程开发的常用三种方式
1.NSThread 2.NSOperationQueue 3.GCD NSThread: 创建方式主要有两种: [NSThread detachNewThreadSelector:@selector ...
- iOS多线程--NSOperation 浅显易懂
NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...
- iOS多线程--NSOperation
NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...
- iOS多线程---NSOperation介绍和使用
1. NSOperation实现多线程编程,需要和NSOperationQueue一起使用. (1)先将要执行的操作封装到NSOperation中 (2)将NSOperation对象添加到NSOpe ...
- IOS 多线程 NSOperation GCD
1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...
- iOS开发NSOperation 三:操作依赖和监听以及线程间通信
一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...
随机推荐
- 淘宝星星的思路c#
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- springmvc cfx 整合
1.先看一下目录列表,本人用的是eclipse 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...
- 专2-第一课 Ubuntu系统安装与配置
1.1 使用VMware安装Ubuntu 1.1.1 准备工作 1)VMware的安装包 VMware至少要用10.0版本,本文采用最新的VMware12版本,这个版本对USB3.0的支持更加完善稳定 ...
- 做SEO都需要具备哪些方面的知识
做seo需要了解的基本知识有利于seo工作的进行 一.了解搜索引擎的工作原理 搜索引擎的基本工作原理包括如下三个过程: 1.首先在互联网中发现.搜集网页信息; 2.同时对信息进行提取和组织建立索引库; ...
- 2018.09.08 NOIP模拟 division(状压dp)
这么sb的题考场居然写挂了2233. 假设n=∏iaiki" role="presentation" style="position: relative;&qu ...
- 2018.08.30 NOIP模拟 kfib(矩阵快速幂+exgcd)
[输入] 一行两个整数 n P [输出] 从小到大输出可能的 k,若不存在,输出 None [样例输入 1] 5 5 [样例输出] 2 [样例解释] f[0] = 2 f[1] = 2 f[2] = ...
- phalApi框架打印SQL语句
http://demo.phalapi.net/?service=User.getBaseInfo&user_id=1&__sql__=1
- Python 字典(Dictionary) keys()方法
Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键. 语法 keys()方法语法: dict.keys() 参数 NA. 返回值 返回一个字典所有的键. 实例 以 ...
- Spring3.x错误--Pointcut is not well-formed:expecting 'name pattern' at...
Spring3.x错误: 解决方法: (*com.dayang.service..*(..)) *和com.dayang.之间有空格
- scrapy windows 安装
windows 7 系统下参照官网安装总是会提示出错,现在整理一下安装的流程 1.安装 python 2.7,添加环境变量 C:\Python27\;C:\Python27\Scripts\; 在 C ...