一、NSOperation簡介
1.簡單說明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能實現多線程編程
NSOperation和NSOperationQueue實現多線程的具體步驟:
(1)先將需要執行的操作封裝到一個NSOperation對象中
(2)然後將NSOperation對象添加到NSOperationQueue中
(3)系統會⾃動將NSOperationQueue中的NSOperation取出來
(4)將取出的NSOperation封裝的操作放到⼀條新線程中執⾏
2.NSOperation的子類
NSOperation是個抽象類別,並不具備封裝操作的能力,必須使⽤它的子類
使用NSOperation⼦類的方式有3種:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自訂子類繼承NSOperation,實現內部相應的⽅法
二、 具體說明
1.NSInvocationOperation子類
建立對象和執行操作:
複製代碼 代碼如下:
//建立操作對象,封裝要執行的任務
//NSInvocationOperation 封裝操作
NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
//執行操作
[operation start];
說明:一旦執⾏操作,就會調用target的test方法
程式碼範例:
複製代碼 代碼如下:
//
// YYViewController.m
// 01-NSOperation基本1
//
// Created by 孔醫己 on 14-6-25.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//NSOperation:抽象類別,不具備封裝功能
//建立操作對象,封裝要執行的任務
//NSInvocationOperation 封裝操作
NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
//執行操作
[operation start];
}
-(void)test
{
NSLog(@"--test--%@--",[NSThread currentThread]);
}
@end
列印查看:
注意:操作對象預設在主線程中執行,只有添加到隊列中才會開啟新的線程。即預設情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會非同步執行操作
2.NSBlockOperation子類
建立對象和添加操作:
複製代碼 代碼如下:
//建立NSBlockOperation操作對象
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
//......
}];
//添加操作
[operation addExecutionBlock:^{
//....
}];
程式碼範例:
代碼1:
複製代碼 代碼如下:
//
// YYViewController.m
// 02-NSTherad基本2
//
// Created by 孔醫己 on 14-6-25.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立NSBlockOperation操作對象
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
}];
//開啟執行操作
[operation start];
}
@end
列印查看:
代碼2:
複製代碼 代碼如下:
//
// YYViewController.m
// 02-NSTherad基本2
//
// Created by 孔醫己 on 14-6-25.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立NSBlockOperation操作對象
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
}];
//添加操作
[operation addExecutionBlock:^{
NSLog(@"NSBlockOperation1------%@",[NSThread currentThread]);
}];
[operation addExecutionBlock:^{
NSLog(@"NSBlockOperation2------%@",[NSThread currentThread]);
}];
//開啟執行操作
[operation start];
}
@end
注意:只要NSBlockOperation封裝的運算元 > 1,就會非同步執行操作
3.NSOperationQueue
NSOperationQueue的作⽤:NSOperation可以調⽤start⽅法來執⾏任務,但預設是同步執行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動非同步執行NSOperation中的操作
添加操作到NSOperationQueue中,自動執行操作,自動開啟線程
複製代碼 代碼如下:
//建立NSOperationQueue
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
//把操作添加到隊列中
//第一種方式
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
//第二種方式
[queue addOperationWithBlock:^{
NSLog(@"NSBlockOperation3--4----%@",[NSThread currentThread]);
}];
複製代碼 代碼如下:
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;
程式碼範例:
複製代碼 代碼如下:
//
// YYViewController.m
// 03-NSOperation基本3
//
// Created by 孔醫己 on 14-6-25.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立NSInvocationOperation對象,封裝操作
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
//建立對象,封裝操作
NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
}];
[operation3 addExecutionBlock:^{
NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
}];
//建立NSOperationQueue
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
//把操作添加到隊列中
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
}
複製代碼 代碼如下:
-(void)test1
{
NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
}
-(void)test2
{
NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
}
@end
列印效果:
注意:系統自動將NSOperationqueue中的NSOperation對象取出,將其封裝的操作放到一條新的線程中執行。上面的程式碼範例中,一共有四個任務,operation1和operation2分別有一個任務,operation3有兩個任務。一共四個任務,開啟了四條線程。通過任務執行的時間全部都是273可以看出,這些任務是並存執行的。
提示:隊列的取出是有順序的,與列印結果並不矛盾。這就好比,選手A,BC雖然起跑的順序是先A,後B,然後C,但是到達終點的順序卻不一定是A,B在前,C在後。
下面使用for迴圈列印,可以更明顯的看出任務是並發執行的。
程式碼範例:
複製代碼 代碼如下:
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立NSInvocationOperation對象,封裝操作
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
//建立對象,封裝操作
NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
for (int i=0; i<5; i++) {
NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
}
}];
[operation3 addExecutionBlock:^{
for (int i=0; i<5; i++) {
NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
}
}];
//建立NSOperationQueue
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
//把操作添加到隊列中
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
}
-(void)test1
{
for (int i=0; i<5; i++) {
NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
}
}
-(void)test2
{
for (int i=0; i<5; i++) {
NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
}
}
@end
三、並發數
(1)並發數:同時執⾏行的任務數.比如,同時開3個線程執行3個任務,並發數就是3
(2)最大並發數:同一時間最多隻能執行的任務的個數。
(3)最⼤大並發數的相關⽅方法
複製代碼 代碼如下:
- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
說明:如果沒有設定最大並發數,那麼並發的個數是由系統記憶體和CPU決定的,可能記憶體多久開多一點,記憶體少就開少一點。
注意:num的值並不代表線程的個數,僅僅代表線程的ID。
提示:最大並發數不要亂寫(5以內),不要開太多,一般以2~3為宜,因為雖然任務是在子線程進行處理的,但是cpu處理這些過多的子線程可能會影響UI,讓UI變卡。
四、隊列的取消,暫停和恢複
(1)取消隊列的所有操作
複製代碼 代碼如下:
- (void)cancelAllOperations;
提⽰:也可以調用NSOperation的- (void)cancel⽅法取消單個操作
(2)暫停和恢複隊列
複製代碼 代碼如下:
- (void)setSuspended:(BOOL)b; // YES代表暫停隊列,NO代表恢複隊列
- (BOOL)isSuspended; //目前狀態
(3)暫停和恢複的適用場合:在tableview介面,開線程下載遠端網路介面,對UI會有影響,使使用者體驗變差。那麼這種情況,就可以設定在使用者操作UI(如滾動螢幕)的時候,暫停隊列(不是取消隊列),停止滾動的時候,恢複隊列。
五、操作優先順序
(1)設定NSOperation在queue中的優先順序,可以改變操作的執⾏優先順序
複製代碼 代碼如下:
- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;
(2)優先順序的取值
複製代碼 代碼如下:
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8
說明:優先順序高的任務,調用的幾率會更大。
六、操作依賴
(1)NSOperation之間可以設定依賴來保證執行順序,⽐如一定要讓操作A執行完後,才能執行操作B,可以像下面這麼寫
複製代碼 代碼如下:
[operationB addDependency:operationA]; // 操作B依賴於操作
(2)可以在不同queue的NSOperation之間建立依賴關係
注意:不能循環相依性(不能A依賴於B,B又依賴於A)。
(3)程式碼範例
複製代碼 代碼如下:
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立NSInvocationOperation對象,封裝操作
NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
//建立對象,封裝操作
NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
for (int i=0; i<5; i++) {
NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
}
}];
[operation3 addExecutionBlock:^{
for (int i=0; i<5; i++) {
NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
}
}];
//設定作業依賴
//先執行operation2,再執行operation1,最後執行operation3
[operation3 addDependency:operation1];
[operation1 addDependency:operation2];
//不能是相互依賴
// [operation3 addDependency:operation1];
// [operation1 addDependency:operation3];
//建立NSOperationQueue
NSOperationQueue * queue=[[NSOperationQueue alloc]init];
//把操作添加到隊列中
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
}
複製代碼 代碼如下:
-(void)test1
{
for (int i=0; i<5; i++) {
NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
}
}
-(void)test2
{
for (int i=0; i<5; i++) {
NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
}
}
@end
列印查看:
A做完再做B,B做完才做C。
注意:一定要在添加之前,進行設定。
提示:任務添加的順序並不能夠決定執行順序,執行的順序取決於依賴。使用Operation的目的就是為了讓開發人員不再關心線程。
5.操作的監聽
可以監聽一個操作的執行完畢
複製代碼 代碼如下:
- (void (^)(void))completionBlock;
- (void)setCompletionBlock:(void (^)(void))block;
程式碼範例
第一種方式:可以直接跟在任務後面編寫需要完成的操作,如這裡在下載圖片後,緊跟著下載第二張圖片。但是這種寫法有的時候把兩個不相關的操作寫到了一個代碼塊中,代碼的可閱讀性不強。
複製代碼 代碼如下:
#import "YYViewController.h"
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立對象,封裝操作
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);
//.....下載圖片後繼續進行的操作
NSLog(@"--接著下載第二張圖片--");
}];
//建立隊列
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//把任務添加到隊列中(自動執行,自動開線程)
[queue addOperation:operation];
}
@end
第二種方式:
複製代碼 代碼如下:
#import "YYViewController.h"
@interface YYViewController ()
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//建立對象,封裝操作
NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
for (int i=0; i<10; i++) {
NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);
}
}];
//監聽操作的執行完畢
operation.completionBlock=^{
//.....下載圖片後繼續進行的操作
NSLog(@"--接著下載第二張圖片--");
};
//建立隊列
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//把任務添加到隊列中(自動執行,自動開線程)
[queue addOperation:operation];
}
@end
列印查看:
說明:在上一個任務執行完後,會執行operation.completionBlock=^{}程式碼片段,且是在當前線程執行(2)。