NSOperation
是一個抽象的基類,表示一個獨(dú)立的計算單元,可以為子類提供有用且線程安全的建立狀態(tài),優(yōu)先級,依賴和取消等操作。系統(tǒng)已經(jīng)給我們封裝了NSBlockOperation
和NSInvocationOperation
這兩個實(shí)體類。使用起來也非常簡單,我們也可以根據(jù)自己的需求使用自己繼承并定制自己的操作。
NSOperation實(shí)現(xiàn)多線程的使用步驟分為三步:
- 創(chuàng)建任務(wù):先將需要執(zhí)行的操作封裝到一個NSOperation對象中。
- 創(chuàng)建隊列:創(chuàng)建NSOperationQueue對象。
- 將任務(wù)加入到隊列中:然后將NSOperation對象添加到NSOperationQueue中。之后呢,系統(tǒng)就會自動將NSOperationQueue中的NSOperation取出來,在新線程中執(zhí)行操作。
NSOperation是個抽象類,并不能封裝任務(wù)。我們只有使用它的子類來封裝任務(wù)。我們有三種方式來封裝任務(wù)。
- 使用子類NSInvocationOperation
- 使用子類NSBlockOperation
- 定義繼承自NSOperation的子類,通過實(shí)現(xiàn)內(nèi)部相應(yīng)的方法來封裝任務(wù)。
//
// ViewController.m
// NSOperation
//
// Created by soliloquy on 2017/8/10.
// Copyright ? 2017年 soliloquy. All rights reserved.
//
#import "ViewController.h"
#import "PTLCustomOperation.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
NSOperation實(shí)現(xiàn)多線程的使用步驟分為三步:
創(chuàng)建任務(wù):先將需要執(zhí)行的操作封裝到一個NSOperation對象中。
創(chuàng)建隊列:創(chuàng)建NSOperationQueue對象。
將任務(wù)加入到隊列中:然后將NSOperation對象添加到NSOperationQueue中。
之后呢,系統(tǒng)就會自動將NSOperationQueue中的NSOperation取出來,在新線程中執(zhí)行操作。
NSOperation是個抽象類,并不能封裝任務(wù)。我們只有使用它的子類來封裝任務(wù)。我們有三種方式來封裝任務(wù)。
使用子類NSInvocationOperation
使用子類NSBlockOperation
定義繼承自NSOperation的子類,通過實(shí)現(xiàn)內(nèi)部相應(yīng)的方法來封裝任務(wù)。
// 添加依賴 依賴關(guān)系需添加到addOperation代碼之前, 否則無效
*/
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self operationDemo4];
}
//添加NSOperation的依賴對象
- (void)operationDemo4 {
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
// 創(chuàng)建操作
// NSInvocationOperation形式
NSInvocationOperation *invacation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run:) object:@"NSInvocationOperation"];
// block形式
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"block: --- %@", [NSThread currentThread]);
}];
// 自定義形式
PTLCustomOperation *custom = [[PTLCustomOperation alloc]init];
// 添加依賴 依賴關(guān)系需添加到addOperation代碼之前, 否則無效
[block addDependency:invacation];
[custom addDependency:block];
// 添加操作到隊列中:addOperation:
[queue addOperation:invacation]; //== [op1 start]
[queue addOperation:block]; //== [op1 start]
[queue addOperation:custom];
}
- (void)operationDemo3 {
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
for (int i = 0; i < 2; ++i) {
NSLog(@"-----%@", [NSThread currentThread]);
}
}];
}
// 使用NSOperationQueue
- (void)operationDemo2 {
NSLog(@"開始--------------");
/*
```操作如果沒有加入到隊列中 不會開啟子線程 會在主隊列運(yùn)行任務(wù)
```只有加入到NSOperationQueue(addOperation)后 任務(wù)才會開啟子線程
*/
// 創(chuàng)建隊列 隊列是并行隊列 不阻塞當(dāng)前線程
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
// 創(chuàng)建操作
// NSInvocationOperation形式
NSInvocationOperation *invacation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run:) object:@"NSInvocationOperation"];
// block形式
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"block: --- %@", [NSThread currentThread]);
}];
// 自定義形式
PTLCustomOperation *custom = [[PTLCustomOperation alloc]init];
// 添加操作到隊列中:addOperation:
[queue addOperation:invacation]; //== [op1 start]
[queue addOperation:block]; //== [op1 start]
[queue addOperation:custom];
/*
wait:
```YES---阻塞當(dāng)前線程, 需等待隊列任務(wù)執(zhí)行完畢,才會執(zhí)行后面的內(nèi)容
```NO--- 不阻塞當(dāng)前線程 不用等待隊列任務(wù)執(zhí)行完畢就可以執(zhí)行后面的操作
*/
// [queue addOperations:@[invacation, block, custom] waitUntilFinished:NO];
NSLog(@"結(jié)束");
}
/**
NSBlockOperation還提供了一個方法addExecutionBlock:,通過addExecutionBlock:就可以為NSBlockOperation添加額外的操作,這些額外的操作就會在其他線程并發(fā)執(zhí)行。
// 當(dāng)主線程的任務(wù)完成后, 額外的任務(wù)也有可能到主線程執(zhí)行
2017-08-10 15:57:54.682 NSOperation[3704:1469121] 1------<NSThread: 0x61800007b3c0>{number = 1, name = main}
2017-08-10 15:57:54.683 NSOperation[3704:1469121] 4------<NSThread: 0x61800007b3c0>{number = 1, name = main}
2017-08-10 15:57:54.683 NSOperation[3704:1469802] 2------<NSThread: 0x600000262a00>{number = 5, name = (null)}
2017-08-10 15:57:54.684 NSOperation[3704:1475479] 3------<NSThread: 0x61000007c100>{number = 9, name = (null)}
*/
- (void)operationDemo1 {
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// 在主線程
NSLog(@"1------%@", [NSThread currentThread]);
}];
// 添加額外的任務(wù)(在子線程執(zhí)行)
[op addExecutionBlock:^{
NSLog(@"2------%@", [NSThread currentThread]);
}];
[op addExecutionBlock:^{
NSLog(@"3------%@", [NSThread currentThread]);
}];
[op addExecutionBlock:^{
NSLog(@"4------%@", [NSThread currentThread]);
}];
[op start];
}
- (void)run:(id)param {
NSLog(@"invacation: --- %@", [NSThread currentThread]);
}
@end
// PTLCustomOperation.m
// NSOperation
//
// Created by soliloquy on 2017/8/10.
// Copyright ? 2017年 soliloquy. All rights reserved.
//
#import "PTLCustomOperation.h"
@implementation PTLCustomOperation
-(void)main {
/**
* 需要執(zhí)行的任務(wù)
*/
NSLog(@"PTLCustomOperation: %@", [NSThread currentThread]);
}
@end
添加依賴 依賴關(guān)系需添加到addOperation代碼之前, 否則無效.
github: https://github.com/soliloquy-local/NSOperation.git