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 ...
随机推荐
- Jigloo 下载 安装 GUI
这个需要授权,一直不能解决!! 网上找了很多,都觉不能访问,这个可以用Eclipse直接更新的 http://www.cloudgardensoftware.com/jigloo/update-sit ...
- 2018.08.30 花园(期望dp)
题目背景 SCOI2017 DAY2 T1 题目描述 小 A 的花园的长和宽分别是 L,H .小 A 喜欢在花园里做游戏.每次做游戏的时候,他都先把花园均匀分割成 L×H 个小方块,每个方块的长和宽都 ...
- 一个 图片 滚动 飞入的css特效
@keyframes bounceInLeft { from, 60%, 75%, 90%, to {animation-timing-function: cubic-bezier(0.215, 0. ...
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defi ...
- BZOJ 1007 [HNOI2008]水平可见直线 (栈)
1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7940 Solved: 3030[Submit][Sta ...
- UESTC 486 Good Morning (水题+坑!)
题意:给你一行字符串,让你找其中蕴含的“good morning"的次数. 析:看起来很水么,多简单,只有统计一下其中字母的出现的次数,然后除以相应的个数. 但是很不幸的是WA,而且是在te ...
- Python 字典(Dictionary) keys()方法
Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键. 语法 keys()方法语法: dict.keys() 参数 NA. 返回值 返回一个字典所有的键. 实例 以 ...
- (博弈)Simple Game --codeforces--570B
链接: http://codeforces.com/problemset/problem/570/B http://acm.hust.edu.cn/vjudge/contest/view.action ...
- JdbcTemplate详解
1.JdbcTemplate操作数据库 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中.同时,为了支 ...
- 转载:<context-param>与<init-param>的区别与作用
<context-param>的作用:web.xml的配置中<context-param>配置作用1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web ...