1 NSOperation
NSOperation 自身是一個抽象類恨闪,定義了一個要執(zhí)行的工作监署,可以定義一個 NSOperation 的子類來使用痘昌,只需要實現(xiàn) NSOperation 的main
方法,通過start
方法來執(zhí)行任務淹办,默認是同步執(zhí)行的,而如果需要支持并發(fā)工作舍沙,那么 NSOperation 子類還需要重寫其他方法怎栽。
但是對于大多數(shù)業(yè)務來說,只需要使用系統(tǒng)定義的 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 操作執(zhí)行的方法
- (void)operationSelector:(NSDictionary *)dict
{
// 接收傳進來的dict
NSLog(@"dictValue = %@", [dict valueForKey:@"key1"]);
sleep(10); // 加個睡眠模仿耗時操作
NSLog(@"currentThread = %@", [NSThread currentThread]);
NSLog(@"mainThread = %@", [NSThread mainThread]);
}
注意start
方法是在主線程執(zhí)行的,控制臺輸出如下
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
從輸出結果可以看出饲鄙,執(zhí)行的操作方法與調用start
的方法在同一個線程凄诞,并且是同步執(zhí)行的。
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
方法在同一個線程執(zhí)行,并且是同步執(zhí)行的轴咱。
3.2 NSBlockOperation 多線程異步執(zhí)行
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 并不是主線程汛蝙,而且其中的操作也是異步執(zhí)行的。
可以看出如果是通過addExecutionBlock
添加的操作則是多線程異步操作朴肺。
@property (readonly, copy) NSArray<void (^)(void)> *executionBlocks;
這個只讀屬性得到添加進 NSBlockOperation 的所有 Block 窖剑,包括第一個。
4 總結
對于這兩個 Operation 戈稿,如果僅使用同步執(zhí)行操作西土,那么并沒有多大的區(qū)別,一個是使用 selector 回調并可以傳遞參數(shù)進去鞍盗,一個是使用 Block 需了,可根據(jù)實際情況選擇。
但是如果想要使用多線程異步操作橡疼,則應該選擇 NSBlockOperation援所,不過注意只有通過addExecutionBlock
添加的操作才是多線程異步操作。
關于 NSInvocationOperation 和 NSBlockOperation 使用先介紹到這里欣除。下一篇我們通過自定義NSOperation
的子類住拭,來實現(xiàn)更加靈活的方法。