一. NSOperation简介
1. 简单说明
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---<NSThread: 0x6080000775c0>{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------<NSThread: 0x60800006a3c0>{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------<NSThread: 0x608000261240>{number = 4, name = (null)} 2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115571] NSBlockOperation------<NSThread: 0x600000267dc0>{number = 3, name = (null)} 2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115529] NSBlockOperation2------<NSThread: 0x60800007dc00>{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-----"); }];