一. NSOperation简介
简单说明
NSOperation
的作用:配合使用NSOperation
和NSOperationQueue
也能实现多线程编程
NSOperation
和NSOperationQueue
实现多线程的具体步骤:- 先将需要执行的操作封装到一个
NSOperation
对象中 - 然后将
NSOperation
对象添加到NSOperationQueue
中 - 系统会自动将
NSOPerationQueue
中的NSOperation
取出来 - 将取出的
NSOperation
封装的操作放到一条新线程中执行
- 先将需要执行的操作封装到一个
2.NSoperation的子类
- NSOperation
是个抽象类,并不具备封装操作的能力,必须使用它的子类
- 使用NSOperation
子类的方式有三种
NSInvocationOperation
NSBlockOperation
自定义子类继承NSOperation,实现内部相应的方法
二.具体说明
1.NSInvocationOperation
子类
创建对象和执行操作:
NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testTarget) object:nil];
[operation start];
```
说明:一旦执行操作,就会调用`Target`的`testTarget`方法
代码示例:
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testTarget) object:nil];
[operation start];
}
-(void)testTarget {
NSLog(@"-------test---%@---",[NSThread currentThread]);
}
打印结果:
2017-03-21 11:16:05.385 TestNSOperationQueue[3648:99757] ——-test—{number = 1, name = main}—
> 注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程,即默认情况下,如果操作没有放到队列中queue中,都是同步执行,只有将`NSOperation`放到一个`NSOperationQueue`中,才会以异步执行
2.**`NSBlockOperation`**子类
1. 创建对象和添加操作:
NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
//......
}];
operation addExecutionBlock:^{
//......
};
2. 代码示例:
- 代码1:
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@”NSBlockOperation——%@”,[NSThread currentThread]);}];
[operation start];
}
@end
打印结果:
2017-03-21 11:37:21.540 TestNSOperationQueue[4033:113489] NSBlockOperation——{number = 1, name = main}
- 代码2:
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@”NSBlockOperation——%@”,[NSThread currentThread]);}];
[operation addExecutionBlock:^{
NSLog(@”NSBlockOperation1——%@”,[NSThread currentThread]);
}];[operation addExecutionBlock:^{
NSLog(@”NSBlockOperation2——%@”,[NSThread currentThread]);
}];[operation start];
}
@end
打印结果:
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115570] NSBlockOperation1——{number = 4, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115571] NSBlockOperation——{number = 3, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115529] NSBlockOperation2——{number = 1, name = main}
> 注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
3.**`NSOperationQueue`**
` NSOperationQueue`的作用:`NSOperation`可以调用`start`方法来执行,但默认是同步执行的
> 如果将`NSOperation`添加到`NSOperationQueue`(操作队列)中,系统会自动异步执行`NSOperation`中的操作
添加操作到`NSOperationQueue`中,自动执行操作,自动开启线程
NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];
NSInvocationOperation * operation3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation3) object:nil];
//创建NSOperationQueue
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//把操作添加到队列中
//第一种方式
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
//第二种方式
[queue addOperationWithBlock:^{
NSLog(@"-------testOperationBlock-----");
}];
代码示例:
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];//创建NSInvocationOperation对象,封装操作
NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];//创建对象,封装操作
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@”—–NSBlockOperation–3–1—- %@”,[NSThread currentThread]);
}];[operation3 addExecutionBlock:^{
NSLog(@”—–NSBlockOperation–3–2—- %@”,[NSThread currentThread]);
}];//创建NSOperationQueue
NSOperationQueue * queue = [[NSOperationQueue alloc]init];//把操作添加到队列中
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
}
-(void)testOperation1
{
NSLog(@”—–NSInvocationOperation–1—- %@”,[NSThread currentThread]);
}
-(void)testOperation2
{
NSLog(@”—–NSInvocationOperation–2—- %@”,[NSThread currentThread]);
}
@end
打印效果:
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159392] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159391] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159394] —–NSBlockOperation–3–1—- {number = 5, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159414] —–NSBlockOperation–3–2—- {number = 6, name = (null)}
> 注意:系统自动将`NSOperationQueue中`的`NSOperation`对象取出,将其封装的操作放到一条新的线程中执行,上面的代码示例一共有四个任务,`operation1`和`operation2`分别有一个任务,`operation3`有2个任务。一共四个任务,开启了四条线程,通过任务执行的时间全部是`2017-03-21 13:36:39.594`可以看出,这些任务是`并行`执行的
>提示:队列的取出是有顺序的,与打印结果并不矛盾。这就好比,选手A,B,C虽然起跑的顺序是先A,后B,然后C,但是到达终点的顺序却不一定是A,B在前,C在后。
下面使用for循环打印,可以更明显的看出任务是并行执行的
代码示例:
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];//创建NSInvocationOperation对象,封装操作
NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];//创建对象,封装操作
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{for (int i = 0; i < 5; i++) { NSLog(@"-----NSBlockOperation----3--1---- %@",[NSThread currentThread]); }
}];
[operation3 addExecutionBlock:^{
for (int i = 0; i < 5; i++) {
NSLog(@”—–NSBlockOperation—-3–2—- %@”,[NSThread currentThread]);
}}];
//创建NSOperationQueue
NSOperationQueue * queue = [[NSOperationQueue alloc]init];//把操作添加到队列中
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
}
-(void)testOperation1
{
for (int i = 0; i < 5; i++) {
NSLog(@”—–NSInvocationOperation–1—- %@”,[NSThread currentThread]);
}
}
-(void)testOperation2
{
for (int i = 0; i < 5; i++) {
NSLog(@”—–NSInvocationOperation–2—- %@”,[NSThread currentThread]);
}
}
@end
打印结果:
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.090 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.094 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.099 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.100 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.101 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.106 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.108 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.112 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.113 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.117 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.119 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
实战: 实现一个下载图片的例子
//
// ViewController.m
// TestNSOperationQueue
//
// Created by taobaichi on 2017/3/21.
// Copyright © 2017年 MaChao. All rights reserved.
//
import “ViewController.h”
define kURL @”http://avatar.csdn.net/2/C/D/1_totogo2010.jpg”
@interface ViewController ()
@property (nonatomic, strong) UIImageView * imagView;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];self.imagView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 50, self.view.frame.size.height/2 - 50, 100, 100)];
self.imagView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.imagView];//创建一个后台线程,后台线程执行downloadImage方法
NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];//创建NSOperationQueue
NSOperationQueue * queue = [[NSOperationQueue alloc]init];//把创建的后台线程放到NSOperationQueue中
[queue addOperation:operation];
}
-(void)downloadImage:(NSString *)url
{
NSLog(@”url: %@”,url);
NSURL * nsUrl = [NSURL URLWithString:url];
NSData * data = [[NSData alloc]initWithContentsOfURL:nsUrl];
UIImage * image = [[UIImage alloc]initWithData:data];
//下载完成后执行主线程updateUI方法
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
-(void)updateUI:(UIImage *)image
{
self.imagView.image = image;
}
@end
“`
代码注释:
1. viewDidLoad方法里可以看到我们用NSInvocationOperation建了一个后台线程,并且放到NSOperationQueue中。后台线程执行downloadImage方法。
2. downloadImage 方法处理下载图片的逻辑。下载完成后用performSelectorOnMainThread执行主线程updateUI方法。
3. updateUI 并把下载的图片显示到图片控件中。
运行后可以看到下载图片显示在界面上