1.最大并发数:

- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt; 
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = ;
注:系统一般会根据内存大小自动设置并发数目,也可以自己设定,但是不要乱设,一般不要超过5个。
 
2.暂停和取消

(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的常用操作的更多相关文章

  1. iOS 多线程 NSOperation、NSOperationQueue

    1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...

  2. iOS开发:UINavigationController常用操作

    NavigationController常用操作: 更改bar的背景颜色:self.navigationController?.navigationBar.barTintColor =UIColor. ...

  3. iOS多线程 NSOperation的用法

    上一篇写了 GCD 的使用,接下来就了解一下 NSOperation ,NSOperation是苹果对 GCD 的 OC 版的一个封装,但是相对于GCD来说可控性更强,并且可以加入操作依赖. NSOp ...

  4. ios多线程开发的常用三种方式

    1.NSThread 2.NSOperationQueue 3.GCD NSThread: 创建方式主要有两种: [NSThread detachNewThreadSelector:@selector ...

  5. iOS多线程--NSOperation 浅显易懂

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  6. iOS多线程--NSOperation

    NSOperation是基于GCD的一套多线程实现方案,和GCD一样,线程的生命周期是由系统来自动管理的,不用像NSThread和Pthread一样让程序员手动管理.相对于GCD来说,它更加地面向对象 ...

  7. iOS多线程---NSOperation介绍和使用

    1.  NSOperation实现多线程编程,需要和NSOperationQueue一起使用. (1)先将要执行的操作封装到NSOperation中 (2)将NSOperation对象添加到NSOpe ...

  8. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...

  9. iOS开发NSOperation 三:操作依赖和监听以及线程间通信

    一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...

随机推荐

  1. ocilib初体验

    #ocilib下载 http://sourceforge.net/projects/orclib/files/ #安装 tar -zxvf ocilib-3.9.3-gnu.tar.gz ./conf ...

  2. KOBAS

    1. What is KOBAS 3.0? KOBAS (KEGG Orthology Based Annotation System) is a web server for gene/protei ...

  3. 2018.09.18 atcoder Many Formulas(搜索)

    传送门 感觉自己搜索能力退化了,这种弱智搜索写了整整5min,这样下去比赛会凉的. 看来得多练练题了. 代码: #include<bits/stdc++.h> #define ll lon ...

  4. Win窗口坐标二维坐标与OpenGl的世界坐标系的之间的相互转换

    Win窗口坐标二维坐标与OpenGl的世界坐标系的转换 几何处理管线擅长于使用视图和投影矩阵以及用于裁剪的视口把顶点的世界坐标变换为窗口坐标. 但是,在有些情况下,需要逆转这个过程.一种常见的情形是: ...

  5. 二:nodejs+express+redis+bootstrap table+jquery UI

    介绍:做一个量化投资的实时系统. 综合: 添加记录,顺序改变的话,refresh之后,能正常刷新吗?可以正常刷新,只是顺序又变回去. express中用fs readfile 时,需要用path.jo ...

  6. ORACLE EBS xml publisher 报表输出字符字段前部"0"被EXCEL自动去掉问题

    http://www.cnblogs.com/lzsu1989/archive/2012/10/17/2728528.html   Oracle  EBS 提供多种报表的开发和输出形式,由于MS Ex ...

  7. 轻松转移github项目步骤

    之前有一些项目是托管在github上的,无奈github速度太慢,而且空间有限,还不能有私有项目.后来发现开源中国的git托管(git.oschina.net)还不错,可以托管1000个项目,而且可以 ...

  8. 别具光芒Div CSS 读书笔记(一)

    继承 边框(border).边界(margin).填充(padding).背景(background) 是不能继承的.   table 中td不会继承body的属性,因此需要单独指定.   权重   ...

  9. Javascript设计模式理论与实战:状态模式

    在软件开发中,很大部分时候就是操作数据,而不同数据下展示的结果我们将其抽象出来称为状态,我们平时开发时本质上就是对应用程序的各种状态进行切换并作出相应处理.状态模式就是一种适合多种状态场景下的设计模式 ...

  10. gdb用法

    mickole@test:~/ctest/05gdb$ gdb simple //开始gdb调试 GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4- ...