-
NSOperation的作用
-
配合使用NSOperation和NSOperationQueue也能實(shí)現(xiàn)多線程編程
-
具體實(shí)現(xiàn)步驟
-
先將需要執(zhí)行的操作封裝到一個(gè)NSOperation對(duì)象中
-
然后將NSOperation對(duì)象添加到NSOperationQueue中
-
系統(tǒng)會(huì)自動(dòng)將NSOperationQueue中的NSOperation取出來(lái)
-
將取出的NSOperation封裝的操作放到一條新線程中執(zhí)行
-
NSOperation是一個(gè)抽象類(lèi),并不具備封裝操作的能力,必須使用其子類(lèi)
-
使用NSOperation子類(lèi)的方式有三種
-
NSInvocationOperation
-
NSBlockOperation
-
自定義子類(lèi)繼承NSOperation,實(shí)現(xiàn)內(nèi)部響應(yīng)的方法
廢話不多說(shuō)上代碼
-
NSInvocationOperation單獨(dú)使用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建InvocationOperation 對(duì)象
//注意: 如果沒(méi)有放入隊(duì)列中的話需要手動(dòng)啟動(dòng)
//
/*
第一個(gè)參數(shù): 目標(biāo)對(duì)象
第二個(gè)參數(shù): 調(diào)用目標(biāo)對(duì)象的那個(gè)方法
第三個(gè)參數(shù): 目標(biāo)方法需要傳入的參數(shù), 如果沒(méi)有參數(shù)寫(xiě)nil
*/
NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocationOperation) object:nil];
//啟動(dòng)任務(wù)
//默認(rèn)情況下(在沒(méi)有被放入隊(duì)列中時(shí)),調(diào)用start方法,是在當(dāng)前線程下執(zhí)行任務(wù),等任務(wù)執(zhí)行完畢才會(huì)繼續(xù)往下執(zhí)行(會(huì)阻塞當(dāng)前線程).
//start 底層其實(shí)就是去調(diào)用 main 方法 [operation main]
[operation start];
}
-
NSInvocationOperation配合NSOperationQueue使用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建NSOperationQueue對(duì)象
//直接alloc/init手動(dòng)創(chuàng)建的NSOperationQueue對(duì)象,是在子線程中執(zhí)行,默認(rèn)并發(fā)執(zhí)行
//[NSOperationQueue mainQueue]創(chuàng)建出來(lái)的NSOperationQueue對(duì)象,是在主線程中執(zhí)行
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
NSInvocationOperation * invocation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocationOperation) object:nil];
NSInvocationOperation * invocation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocationOperation) object:nil];
//添加到隊(duì)列后,就不需要再手動(dòng)start了
//異步執(zhí)行
//注意:如果是主隊(duì)列,并且當(dāng)前線程正好也是主線程的話,會(huì)先執(zhí)行完所有代碼,然后再去執(zhí)行隊(duì)列中的任務(wù)
[queue addOperation:invocation1];
[queue addOperation:invocation2];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建BlockOperation對(duì)象
//注意: 如果沒(méi)有放入隊(duì)列中的話需要手動(dòng)啟動(dòng)
NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1==%@",[NSThread currentThread]);
}];
//通過(guò)addExecutionBlock方法動(dòng)態(tài)添加多個(gè)任務(wù)
[operation addExecutionBlock:^{
NSLog(@"2==%@",[NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"3==%@",[NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"4==%@",[NSThread currentThread]);
}];
//啟動(dòng)任務(wù)
//默認(rèn)情況下(在沒(méi)有被放入隊(duì)列中時(shí)),調(diào)用start方法,會(huì)等所有任務(wù)執(zhí)行完畢才會(huì)繼續(xù)執(zhí)行下去,也就是說(shuō)會(huì)阻塞當(dāng)前線程
//最先添加的任務(wù)是在當(dāng)前線程中執(zhí)行, 后添加的任務(wù)是在子線程中執(zhí)行
//start 底層其實(shí)就是去調(diào)用 main 方法 [operation main]
[operation start];
NSLog(@"end");
}
-
NSBlockOperation配合NSOperationQueue使用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建NSOperationQueue對(duì)象
//直接alloc/init手動(dòng)創(chuàng)建的NSOperationQueue對(duì)象,是在子線程中執(zhí)行,默認(rèn)并發(fā)執(zhí)行
//[NSOperationQueue mainQueue]創(chuàng)建出來(lái)的NSOperationQueue對(duì)象,是在主線程中執(zhí)行
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
//添加到隊(duì)列后,就不需要再手動(dòng)start了
//異步執(zhí)行
//注意:如果是主隊(duì)列,并且當(dāng)前線程正好也是主線程的話,會(huì)先執(zhí)行完所有代碼,然后再去執(zhí)行隊(duì)列中的任務(wù)
[queue addOperation:operation];
//直接通過(guò)queue的addOperationWithBlock方法來(lái)添加任務(wù)
[queue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
}
-
自定義子類(lèi)繼承NSOperation,實(shí)現(xiàn)內(nèi)部響應(yīng)的方法
-
前面說(shuō)過(guò)執(zhí)行任務(wù)的底層實(shí)現(xiàn)其實(shí)就是調(diào)用NSOperation對(duì)象的main方法.
-
所有我們只要自定一個(gè)類(lèi)繼承NSOperation,重寫(xiě)main,把任務(wù)代碼編寫(xiě)在面方法中即可.系統(tǒng)自帶的NSInvocationOperation和NSBlockOperation的怎么用,自定義的NSOperation也就怎么用.
#import <Foundation/Foundation.h>
@interface YXOperation : NSOperation
@end
@implementation YXOperation
-(void)main{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"耗時(shí)操作代碼........");
}
@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 創(chuàng)建自定義的NSOperation對(duì)象
YXOperation * operation1 = [[YXOperation alloc] init];
YXOperation * operation2 = [[YXOperation alloc] init];
YXOperation * operation3 = [[YXOperation alloc] init];
YXOperation * operation4 = [[YXOperation alloc] init];
//把Operation對(duì)象封裝到數(shù)組中
NSArray * operationArray = @[operation1,operation2,operation3,operation4];
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
//把NSOperation對(duì)象數(shù)組添加到隊(duì)列中
/*
第一個(gè)參數(shù): NSOperation 對(duì)象數(shù)組
第二個(gè)參數(shù): 是否等待所有任務(wù)執(zhí)行完畢, YES(等待,會(huì)阻塞當(dāng)前線程) NO(不等待)
*/
[queue addOperations:operationArray waitUntilFinished: NO];
NSLog(@"end");
}
-
隊(duì)列的暫停,恢復(fù),取消,最大并發(fā)數(shù)
//按鈕點(diǎn)擊事件
- (IBAction)btnClick:(id)sender {
//設(shè)置與隊(duì)列當(dāng)前狀態(tài)相反的值
//執(zhí)行狀態(tài)就暫停,暫停狀態(tài)就恢復(fù)
//暫停之后,會(huì)把當(dāng)前正在執(zhí)行的任務(wù)執(zhí)行完,然后不再執(zhí)行任務(wù)
//恢復(fù)之后,會(huì)接著繼續(xù)執(zhí)行沒(méi)有執(zhí)行的任務(wù)
self.queue.suspended = !self.queue.suspended;
//取消隊(duì)列中所有的任務(wù)
//注意: 取消之后就不能再恢復(fù)了,當(dāng)前正在執(zhí)行的任務(wù)不會(huì)被取消,
//底層其實(shí)就是調(diào)用隊(duì)列中所有NSOperation任務(wù)對(duì)象的cancel方法,可以通過(guò)隊(duì)列的operations屬性獲取隊(duì)列中所有NSOperations對(duì)象(self.queue.operations)
//開(kāi)發(fā)中,自定義Operation的時(shí)候在重寫(xiě)main方式,做耗時(shí)操作時(shí),在適當(dāng)?shù)奈恢门袛嘤脩羰欠袢∠巳蝿?wù),如果取消了直接return任務(wù)
//self.queue cancelAllOperations];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.queue = [[NSOperationQueue alloc] init];
//最大并發(fā)數(shù):最大同時(shí)執(zhí)行的任務(wù)數(shù)
// 1.默認(rèn)是-1沒(méi)有限制,由系統(tǒng)自己決定
// 2.不要設(shè)置為0,設(shè)置為0就不會(huì)執(zhí)行任務(wù)了
// 3.設(shè)置為1,就可以達(dá)到串行的效果
self.queue.maxConcurrentOperationCount = 1;
[self.queue addOperationWithBlock:^{
NSLog(@"==============================================");
for(int i = 0 ; i < 10000; i++){
NSLog(@"%d*****%@", i ,[NSThread currentThread]);
}
}];
[self.queue addOperationWithBlock:^{
NSLog(@"==============================================");
for(int i = 0 ; i < 10000; i++){
NSLog(@"%d*****%@", i ,[NSThread currentThread]);
}
}];
[self.queue addOperationWithBlock:^{
NSLog(@"==============================================");
for(int i = 0 ; i < 10000; i++){
NSLog(@"%d*****%@", i ,[NSThread currentThread]);
}
}];
[self.queue addOperationWithBlock:^{
NSLog(@"==============================================");
for(int i = 0 ; i < 10000; i++){
NSLog(@"%d*****%@", i ,[NSThread currentThread]);
}
}];
}
-
NSOperation的依賴(lài)和監(jiān)聽(tīng)
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建隊(duì)列
NSOperationQueue * queue1 = [[NSOperationQueue alloc] init];
NSOperationQueue * queue2 = [[NSOperationQueue alloc] init];
//創(chuàng)建任務(wù)
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1==%@",[NSThread currentThread]);
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2==%@",[NSThread currentThread]);
}];
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3==%@",[NSThread currentThread]);
}];
NSBlockOperation * operation4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"4==%@",[NSThread currentThread]);
}];
//添加依賴(lài),并需要添加到隊(duì)列之前添加依賴(lài)
//依賴(lài)了誰(shuí),就會(huì)在誰(shuí)執(zhí)行完之后再執(zhí)行
//可以跨隊(duì)列: 依賴(lài)對(duì)象和被依賴(lài)對(duì)象可以不再一個(gè)隊(duì)列
[operation4 addDependency:operation1];
[operation4 addDependency:operation2];
[operation4 addDependency:operation3];
//添加監(jiān)聽(tīng)
//任務(wù)執(zhí)行完之后,就會(huì)調(diào)用這個(gè)block,在子線程中執(zhí)行
[operation4 setCompletionBlock:^{
NSLog(@"operation4執(zhí)行完畢. %@",[NSThread currentThread]);
}];
//把任務(wù)添加到隊(duì)列中
[queue1 addOperation:operation1];
[queue1 addOperation:operation2];
[queue2 addOperation:operation3];
[queue2 addOperation:operation4];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
//耗時(shí)操作
//回到主線程刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//刷新UI
}];
}];
}