转自:http://www.jianshu.com/p/e71973549237
1 NSOperation
NSOperation 自身是一个抽象类,定义了一个要执行的工作,可以定义一个 NSOperation 的子类来使用,只需要实现 NSOperation 的main
方法,通过start
方法来执行任务,默认是同步执行的,而如果需要支持并发工作,那么 NSOperation 子类还需要重写其他方法。
但是对于大多数业务来说,只需要使用系统定义的 NSOperation 的两个子类NSInvocationOperation
和NSBlockOperation
配合NSOperationQueue
即可达到我们的需求。自定义 NSOperation 子类的方法后文再介绍。
2 NSInvocationOperation
先来看看基本用法
- (void)viewDidLoad {
[super viewDidLoad];
// 可以传递一个 NSObject 给operation的操作方法
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"value1" forKey:@"key1"];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operationSelector:) object:dict];
NSLog(@"start before");
[op start];
NSLog(@"start after");
}
// NSInvocationOperation 操作执行的方法
- (void)operationSelector:(NSDictionary *)dict
{
// 接收传进来的dict
NSLog(@"dictValue = %@", [dict valueForKey:@"key1"]);
sleep(10); // 加个睡眠模仿耗时操作
NSLog(@"currentThread = %@", [NSThread currentThread]);
NSLog(@"mainThread = %@", [NSThread mainThread]);
}
注意start
方法是在主线程执行的,控制台输出如下
2015-12-24 12:51:48.369 test[32228:16046453] start before
2015-12-24 12:51:48.369 test[32228:16046453] dictValue = value1
2015-12-24 12:51:58.369 test[32228:16046453] currentThread = <NSThread: 0x7fbfc0600a50>{number = 1, name = main}
2015-12-24 12:51:58.370 test[32228:16046453] mainThread = <NSThread: 0x7fbfc0600a50>{number = 1, name = main}
2015-12-24 12:51:58.370 test[32228:16046453] start after
从输出结果可以看出,执行的操作方法与调用start
的方法在同一个线程,并且是同步执行的。
3 NSBlockOperation
3.1 NSBlockOperation 的基本用法
NSBlockOperation 的使用也非常简单
- (void)viewDidLoad {
[super viewDidLoad];
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
sleep(10); // 加个睡眠模仿耗时操作
NSLog(@"currentThread = %@", [NSThread currentThread]);
NSLog(@"mainThread = %@", [NSThread mainThread]);
}];
NSLog(@"start before");
[op start];
NSLog(@"start after");
}
控制台输出结果如下
2015-12-24 13:01:46.440 test[91193:16257301] start before
2015-12-24 13:01:56.442 test[91193:16257301] currentThread = <NSThread: 0x7fd9aac03f30>{number = 1, name = main}
2015-12-24 13:01:56.442 test[91193:16257301] mainThread = <NSThread: 0x7fd9aac03f30>{number = 1, name = main}
2015-12-24 13:01:56.442 test[91193:16257301] start after
可以看出,NSBlockOperation 与 NSInvocationOperation 的结果是一样的,Block 中的操作与start
方法在同一个线程执行,并且是同步执行的。
3.2 NSBlockOperation 多线程异步执行
NSBlockOperation 还提供了这个方法
- (void)addExecutionBlock:(void (^)(void))block;
在上面的代码基础上扩展一下
- (void)viewDidLoad {
[super viewDidLoad];
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"BlockOperation 1 begin");
sleep(10); // 加个睡眠模仿耗时操作
NSLog(@"BlockOperation 1 currentThread = %@", [NSThread currentThread]);
NSLog(@"BlockOperation 1 mainThread = %@", [NSThread mainThread]);
NSLog(@"BlockOperation 1 end");
}];
[op addExecutionBlock:^{
NSLog(@"BlockOperation 2 begin");
sleep(10);
NSLog(@"BlockOperation 2 currentThread = %@", [NSThread currentThread]);
NSLog(@"BlockOperation 2 mainThread = %@", [NSThread mainThread]);
NSLog(@"BlockOperation 2 end");
}];
[op addExecutionBlock:^{
NSLog(@"BlockOperation 3 begin");
sleep(10);
NSLog(@"BlockOperation 3 currentThread = %@", [NSThread currentThread]);
NSLog(@"BlockOperation 3 mainThread = %@", [NSThread mainThread]);
NSLog(@"BlockOperation 3 end");
}];
NSLog(@"start before");
[op start];
NSLog(@"start after");
}
控制台输出结果
2015-12-24 13:12:33.720 test[91459:16314387] start before
2015-12-24 13:12:33.720 test[91459:16314387] BlockOperation 1 begin
2015-12-24 13:12:33.720 test[91459:16314433] BlockOperation 3 begin
2015-12-24 13:12:33.720 test[91459:16314432] BlockOperation 2 begin
2015-12-24 13:12:43.725 test[91459:16314387] BlockOperation 1 currentThread = <NSThread: 0x7fee1b507ef0>{number = 1, name = main}
2015-12-24 13:12:43.726 test[91459:16314387] BlockOperation 1 mainThread = <NSThread: 0x7fee1b507ef0>{number = 1, name = main}
2015-12-24 13:12:43.726 test[91459:16314387] BlockOperation 1 end
2015-12-24 13:12:43.786 test[91459:16314433] BlockOperation 3 currentThread = <NSThread: 0x7fee1ea08010>{number = 2, name = (null)}
2015-12-24 13:12:43.786 test[91459:16314432] BlockOperation 2 currentThread = <NSThread: 0x7fee1b407cf0>{number = 3, name = (null)}
2015-12-24 13:12:43.786 test[91459:16314432] BlockOperation 2 mainThread = <NSThread: 0x7fee1b507ef0>{number = 1, name = (null)}
2015-12-24 13:12:43.786 test[91459:16314433] BlockOperation 3 mainThread = <NSThread: 0x7fee1b507ef0>{number = 1, name = (null)}
2015-12-24 13:12:43.786 test[91459:16314432] BlockOperation 2 end
2015-12-24 13:12:43.786 test[91459:16314433] BlockOperation 3 end
2015-12-24 13:12:43.786 test[91459:16314387] start after
看到 Block2 和 Block3 中的 currentThread 并不是主线程,而且其中的操作也是异步执行的。
可以看出如果是通过addExecutionBlock
添加的操作则是多线程异步操作。
@property (readonly, copy) NSArray<void (^)(void)> *executionBlocks;
这个只读属性得到添加进 NSBlockOperation 的所有 Block ,包括第一个。
4 总结
对于这两个 Operation ,如果仅使用同步执行操作,那么并没有多大的区别,一个是使用 selector 回调并可以传递参数进去,一个是使用 Block ,可根据实际情况选择。
但是如果想要使用多线程异步操作,则应该选择 NSBlockOperation,不过注意只有通过addExecutionBlock
添加的操作才是多线程异步操作。
关于 NSInvocationOperation 和 NSBlockOperation 使用先介绍到这里。下一篇我们通过自定义NSOperation
的子类,来实现更加灵活的方法。