iOS開發(fā)多線程篇—NSOperation基本操作
(1)取消隊列的所有操作
- (void)cancelAllOperations;
提?:也可以調用NSOperation的- (void)cancel?法取消單個操作
(2)暫停和恢復隊列
- (void)setSuspended:(BOOL)b; // YES代表暫停隊列,NO代表恢復隊列
- (BOOL)isSuspended; //當前狀態(tài)
(1)設置NSOperation在queue中的優(yōu)先級,可以改變操作的執(zhí)?優(yōu)先級
- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;
(2)優(yōu)先級的取值
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8
說明:優(yōu)先級高的任務,調用的幾率會更大。
(1)NSOperation之間可以設置依賴來保證執(zhí)行順序,?如一定要讓操作A執(zhí)行完后,才能執(zhí)行操作B,可以像下面這么寫
[operationB addDependency:operationA]; // 操作B依賴于操作
(2)可以在不同queue的NSOperation之間創(chuàng)建依賴關系
注意:不能循環(huán)依賴(不能A依賴于B,B又依賴于A)。
(3)代碼示例
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad10 {11 [super viewDidLoad];12 13 //創(chuàng)建NSInvocationOperation對象,封裝操作14 NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];15 NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];16 //創(chuàng)建對象,封裝操作17 NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{18 for (int i=0; i<5; i++) {19 NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);20 }21 }];22 [operation3 addExecutionBlock:^{23 for (int i=0; i<5; i++) {24 NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);25 }26 }];27 28 //設置操作依賴29 //先執(zhí)行operation2,再執(zhí)行operation1,最后執(zhí)行operation330 [operation3 addDependency:operation1];31 [operation1 addDependency:operation2];32 33 //不能是相互依賴34 // [operation3 addDependency:operation1];35 // [operation1 addDependency:operation3];36 37 //創(chuàng)建NSOperationQueue38 NSOperationQueue * queue=[[NSOperationQueue alloc]init];39 //把操作添加到隊列中40 [queue addOperation:operation1];41 [queue addOperation:operation2];42 [queue addOperation:operation3];43 }44 45 -(void)test146 {47 for (int i=0; i<5; i++) {48 NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);49 }50 }51 52 -(void)test253 {54 for (int i=0; i<5; i++) {55 NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);56 }57 }58 59 @end
可以監(jiān)聽一個操作的執(zhí)行完畢
- (void (^)(void))completionBlock;
- (void)setCompletionBlock:(void (^)(void))block;
代碼示例
第一種方式:可以直接跟在任務后面編寫需要完成的操作,如這里在下載圖片后,緊跟著下載第二張圖片。但是這種寫法有的時候把兩個不相關的操作寫到了一個代碼塊中,代碼的可閱讀性不強。
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad10 {11 [super viewDidLoad];12 13 //創(chuàng)建對象,封裝操作14 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{15 NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);16 //.....下載圖片后繼續(xù)進行的操作17 NSLog(@"--接著下載第二張圖片--");18 }];19 20 //創(chuàng)建隊列21 NSOperationQueue *queue=[[NSOperationQueue alloc]init];22 //把任務添加到隊列中(自動執(zhí)行,自動開線程)23 [queue addOperation:operation];24 }25 26 @end
第二種方式:
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad10 {11 [super viewDidLoad];12 13 //創(chuàng)建對象,封裝操作14 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{15 for (int i=0; i<10; i++) {16 NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);17 }18 }];19 20 //監(jiān)聽操作的執(zhí)行完畢21 operation.completionBlock=^{22 //.....下載圖片后繼續(xù)進行的操作23 NSLog(@"--接著下載第二張圖片--");24 };25 26 //創(chuàng)建隊列27 NSOperationQueue *queue=[[NSOperationQueue alloc]init];28 //把任務添加到隊列中(自動執(zhí)行,自動開線程)29 [queue addOperation:operation];30 }31 32 @end
打印查看:
說明:在上一個任務執(zhí)行完后,會執(zhí)行operation.completionBlock=^{}代碼段,且是在當前線程執(zhí)行(2)。
聯(lián)系客服