NSOpertation是一套OC的API,是对GCD进行的Cocoa抽象。

NSOperation有两种不同类型的队列,主队列和自定义队列。

主队列运行于主线程上,自定义队列在后台运行。

【NSBlockOperation】

通过Block创建任务,下面比较主队列和自定义队列的区别:

将自定义队列声明为成员变量,并进行初始化:

@property (nonatomic, strong) NSOperationQueue *myqueue;
self.myqueue = [[NSOperationQueue alloc] init];

获取主队列的方法为[NSOperationQueue mainQueue]。

队列有一个方法addOperationWithBlock方法用于添加一个用Block描述的任务。

具体代码为:

- (void)NSBlockOperation{

    // 自定义队列在子线程中运行。
[self.myqueue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
// 主队列任务在主线程中运行。
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}]; }

执行这个方法,得到的结果如下,可见与上面的描述相符。

2015-02-17 10:46:15.308 NSOpertaion[709:17138] <NSThread: 0x7b9aa440>{number = 2, name = (null)}
2015-02-17 10:46:15.319 NSOpertaion[709:17067] <NSThread: 0x7b978550>{number = 1, name = main}

【NSInvocationOperation】

需要定义一个回调方法,好处是可以接收一个id类型的object作为消息。

例如:

- (void)NSInvocationOperation{

    NSDictionary *msg = @{@"name" : @"op",@"message" : @"hello"};
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(InvocationCall:) object:msg];
[self.myqueue addOperation:op]; }

实现回调方法:

- (void)InvocationCall:(id)obj{
NSLog(@"%@ with object %@",[NSThread currentThread],obj);
}

调用后,打印的结果如下,可以看到object对象被传了过来。

2015-02-17 10:56:03.764 NSOpertaion[812:26537] <NSThread: 0x7ba6eb10>{number = 2, name = (null)} with object {
message = hello;
name = op;
}

【任务执行顺序】

在默认情况下,自定义队列是并行队列,执行无序;而主队列为串行队列,有序执行。下面进行实验验证说法:

    // 自定义队列在子线程中运行。
for (int i = 0; i < 9; i++) {
[self.myqueue addOperationWithBlock:^{
NSLog(@"%@ with no %d",[NSThread currentThread],i);
}];
} // 主队列任务在主线程中运行。
for (int i = 0; i < 9; i++) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@ with no %d",[NSThread currentThread],i);
}];
}

执行结果如下,可见与上面的描述相同。

2015-02-17 11:04:08.421 NSOpertaion[919:32337] <NSThread: 0x7bf91f50>{number = 9, name = (null)} with no 6
2015-02-17 11:04:08.421 NSOpertaion[919:32331] <NSThread: 0x7bf91c10>{number = 4, name = (null)} with no 1
2015-02-17 11:04:08.421 NSOpertaion[919:32338] <NSThread: 0x7bf915c0>{number = 6, name = (null)} with no 5
2015-02-17 11:04:08.421 NSOpertaion[919:32336] <NSThread: 0x7dab62c0>{number = 7, name = (null)} with no 4
2015-02-17 11:04:08.421 NSOpertaion[919:32339] <NSThread: 0x7dab61b0>{number = 3, name = (null)} with no 7
2015-02-17 11:04:08.421 NSOpertaion[919:32330] <NSThread: 0x7dab6110>{number = 2, name = (null)} with no 0
2015-02-17 11:04:08.421 NSOpertaion[919:32341] <NSThread: 0x7d97b3f0>{number = 5, name = (null)} with no 8
2015-02-17 11:04:08.421 NSOpertaion[919:32328] <NSThread: 0x7be6d450>{number = 10, name = (null)} with no 3
2015-02-17 11:04:08.421 NSOpertaion[919:32329] <NSThread: 0x7dab6450>{number = 8, name = (null)} with no 2
2015-02-17 11:04:08.448 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 0
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 1
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 2
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 3
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 4
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 5
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 6
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 7
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 8

【自定义队列顺序执行】

使用NSBlockOperation对象的addDependency设置依赖关系,只有依赖的对象执行完毕后,自己才能执行。

例如下面的例子,三个任务要顺序执行,先下载,再处理,最后显示,通过这样的设定可以保证顺序:

- (void)SerialOperation{

    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"下载");
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"处理");
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"显示");
}]; [op2 addDependency:op1];
[op3 addDependency:op2]; [self.myqueue addOperation:op1];
[self.myqueue addOperation:op2];
[self.myqueue addOperation:op3]; }

注意这个执行和串行队列的异步任务不同点是,串行队列的异步任务仅仅开一个线程;自定义队列的顺序执行可能开辟多个但不会太多个线程。

注意上面的代码有一定的问题,因为显示只有主线程可以处理,所以op3应该放入主线程。

Tip:依赖关系可以跨队列,因此op3依赖op2在主线程中仍然有效,只需要修改op3的入队代码为:

[[NSOperationQueue mainQueue] addOperation:op3];

Tip:注意避开循环依赖,程序会崩溃。

【设定多线程的最大开销】

设定同时执行的最大线程数:通过队列的setMaxConcurrentOperationCount方法来设定,例如:

[self.myqueue setMaxConcurrentOperationCount:3];

应用场景:网络通信,例如3G开3个子线程,WIFI开6个子线程。

Tip:线程的开销主要是CPU和内存,还会耗电,因此应该考虑软件的能耗。

Tip:AFNetworing的底层是使用GCD开发的,接口是NSOperation。

(五十六)iOS多线程之NSOperation的更多相关文章

  1. iOS 多线程之NSOperation篇举例详解

    这篇博客是接着总篇iOS GCD NSOperation NSThread等多线程各种举例详解写的一个支篇.总篇也包含了此文的链接.本文讲解的知识点有NSBlockOperationClick,队列, ...

  2. iOS多线程之NSOperation详解

    使用NSOperation和NSOperationQueue进行多线程开发,只要将一个NSOperation(实际开发中需要使用其子类 NSInvocationOperation,NSBlockOpe ...

  3. IOS多线程之NSOperation学习总结

    NSOperation简介 1.NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 2.NSOperation和NSOperationQu ...

  4. iOS多线程之NSOperation,NSOperationQueue

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  5. iOS多线程之NSOperation和NSOperationQueue的使用

    一:NSOperation 两个子类+重写main方法 NSInvocationOperation NSBlockOperation 有个类方法 BlockOprationWith: 还有就是自己个子 ...

  6. ios多线程之NSOperation

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  7. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  8. iOS多线程之GCD小记

    iOS多线程之GCD小记 iOS多线程方案简介 从各种资料中了解到,iOS中目前有4套多线程的方案,分别是下列4中: 1.Pthreads 这是一套可以在很多操作系统上通用的多线程API,是基于C语言 ...

  9. 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...

随机推荐

  1. Python中的数据类型

    计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数 ...

  2. 推荐几种PHP实现页面跳转的方法

    1.PHP实现页面跳转第一种方法 <?php header("Location:http://www.baidu.com"); ?> header()是php内置函数, ...

  3. ACM hdu 3336 Count the string

    [题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...

  4. ACM Sudoku

    Sudoku是一个非常简单的任务. 具有9行9列的方形表被划分为9个较小的正方形3x3,如图所示. 在一些单元格中写入从1到9的十进制数字.其他单元格为空. 目标是填充空单元格,其中十进制数字从1到9 ...

  5. Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费

    Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费 其实在很早的时候我就有想过,是不是退出一些群,因为群太多了,里面的水友也多,基友也多,就难免会水起来,这样既耽误学习又耽误工作, ...

  6. 为什么内部类访问的外部变量需要使用final修饰

    因为生命周期的原因.方法中的局部变量,方法结束后这个变量就要释放掉,final保证这个变量始终指向一个对象.首先,内部类和外部类其实是处于同一个级别,内部类不会因为定义在方法中就会随着方法的执行完毕而 ...

  7. 20160209.CCPP体系详解(0019天)

    程序片段(01):01.字符串.c 内容概要:字符串 #include <stdio.h> #include <stdlib.h> #include <locale.h& ...

  8. Unity UGUI图文混排(六) -- 超链接

    图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...

  9. [csdn markdown]使用摘记二 快捷键及导入导出Markdown文件

    csdn推出了新的编辑器markdown,对于习惯使用离线编辑的人员来说是个大的福利,比如上班的时候,不能联网,但是又有好些知识点需要记录,等到下班了呢,又想直接把这些排版格式良好的文件直接上传到cs ...

  10. Android在一个TextView里显示不同样式的字体

    在同一个TextView里显示不同样式的字体 public void setSpan(Object what, int start, int end, int flags); 样式1:背景色.粗体.字 ...