iOS開發(fā) 自定義NSOPeration

iOS多線程開發(fā)中,NSOperation是我們經(jīng)常使用的,有時系統(tǒng)自帶的兩個類NSInvocationOperation和NSBlockOperation不能滿足我們的需求時就需要我們自定義。
自定義NSOperation分兩種,一種是自定義非并發(fā)的NSOperation,一種是定義并發(fā)的NSOperation的。下面分別介紹。
一:定義非并發(fā)的NSOperation。
定義非并發(fā)的NSOperation的比較簡單:
1.實現(xiàn)main方法,在main方法中執(zhí)行自定義的任務
2.正確的響應取消事件
具體代碼如下:
在ZCNoCurrentOperation.h文件中:

//  ZCNoCurrentOperation.h  
//  自定義非并發(fā)NSOPeration  
//  Created by MrZhao on 16/9/13.  
//  Copyright ? 2016年 MrZhao. All rights reserved.  
/* 
*自定義非并發(fā)的NSOperation步驟:1.實現(xiàn)main方法,在main方法中執(zhí)行自定義的任務 
 2.正確的響應取消事件 
*/    
#import <Foundation/Foundation.h> 
@interface ZCNoCurrentOperation : NSOperation  
@end  

在ZCNoCurrentOperation.m中要注意兩點1.創(chuàng)建釋放池,2.正確相應取消事件。

//  ZCNoCurrentOperation.m  
//  自定義非并發(fā)NSOPeration  
//  
//  Created by MrZhao on 16/9/13.  
//  Copyright ? 2016年 MrZhao. All rights reserved.  
#import "ZCNoCurrentOperation.h"  
@implementation ZCNoCurrentOperation  
/*自定義main方法執(zhí)行你的任務*/  
- (void)main {  
    //捕獲異常  
    @try {  
        //在這里我們要創(chuàng)建自己的釋放池,因為這里我們拿不到主線程的釋放池  
        @autoreleasepool {  
            BOOL isDone = NO;  
            //正確的響應取消事件  
            while(![self isCancelled] && !isDone)  
            {  
                //在這里執(zhí)行自己的任務操作  
                NSLog(@"執(zhí)行自定義非并發(fā)NSOperation");  
                NSThread *thread = [NSThread currentThread];  
                NSLog(@"%@",thread);  
                  
                //任務執(zhí)行完成后將isDone設為YES  
                isDone = YES;  
            }  
        }     
    }  
    @catch (NSException *exception) {      
    }       
} 
@end 

我們在ViewDidLoad函數(shù)中使用我們自定義的operation,并調(diào)用start方法

(void)viewDidLoad {  
   [super viewDidLoad];  
   ZCNoCurrentOperation *operation1 = [[ZCNoCurrentOperation alloc] init];  
   //默認情況下,該operati  
   [operation1 start];
}

打印結(jié)果如下:

20160913161731455.png

默認情況下,該operation在當前調(diào)用start的線程中執(zhí)行.其實如果我們創(chuàng)建多個自定義的ZCNoCurrentOperation,并放入NSOperationQueue中,這些任務也是并發(fā)執(zhí)行的,只不過因為我們沒有處理并發(fā)情況下,線程執(zhí)行完,KVO等操作,因此不建議在只實現(xiàn)main函數(shù)的情況下將其加入NSOperationQueue,只實現(xiàn)main一般只適合自定義非并發(fā)的。

#import "ViewController.h"  
#import "ZCNoCurrentOperation.h"  
@interface ViewController ()  
@property(nonatomic,strong)NSOperationQueue *myQueue;  
@end  
@implementation ViewController  
- (void)viewDidLoad {  
    [super viewDidLoad];  
    self.myQueue = [[NSOperationQueue alloc] init];  
    ZCNoCurrentOperation *operation1 = [[ZCNoCurrentOperation alloc] init];  
    [self.myQueue addOperation:operation1];  
    ZCNoCurrentOperation *operation2 = [[ZCNoCurrentOperation alloc] init];  
    [self.myQueue addOperation:operation2];  
    ZCNoCurrentOperation *operation3 = [[ZCNoCurrentOperation alloc] init];  
    [self.myQueue addOperation:operation3];  
}  
@end 
20160913163205563.png

二:自定義并發(fā)的NSOperation
自定義并發(fā)的NSOperation需要以下步驟:
1.start方法:該方法必須實現(xiàn),
2.main:該方法可選,如果你在start方法中定義了你的任務,則這個方法就可以不實現(xiàn),但通常為了代碼邏輯清晰,通常會在該方法中定 義自己的任務
3.isExecuting isFinished 主要作用是在線程狀態(tài)改變時,產(chǎn)生適當?shù)腒VO通知
4.isConcurrent :必須覆蓋并返回YES;
下面給出代碼:
在ZCCurrentOperation.h中

//  ZCCurrentOperation.h  
//  自定義非并發(fā)NSOPeration  
//  Created by MrZhao on 16/9/13.  
//  Copyright ? 2016年 MrZhao. All rights reserved.  
/* 
 *自定義并發(fā)的NSOperation需要以下步驟: 
 1.start方法:該方法必須實現(xiàn), 
 2.main:該方法可選,如果你在start方法中定義了你的任務,則這個方法就可以不實現(xiàn),但通常為了代碼邏輯清晰,通常會在該方法中定義自己的任務 
 3.isExecuting  isFinished 主要作用是在線程狀態(tài)改變時,產(chǎn)生適當?shù)腒VO通知 
 4.isConcurrent :必須覆蓋并返回YES; 
 */  
#import <Foundation/Foundation.h>  
@interface ZCCurrentOperation : NSOperation {  
    BOOL executing;  
    BOOL finished;  
}  
@end  

在ZCCurrentOperation.m中

//  ZCCurrentOperation.m  
//  自定義非并發(fā)NSOPeration  
//  Created by MrZhao on 16/9/13.  
//  Copyright ? 2016年 MrZhao. All rights reserved.  
#import "ZCCurrentOperation.h"  
@implementation ZCCurrentOperation  
- (id)init {  
    if(self = [super init])  
    {  
        executing = NO;  
        finished = NO;  
    }  
    return self;  
}  
- (BOOL)isConcurrent {  
    return YES;  
}  
- (BOOL)isExecuting {    
    return executing;  
}  
- (BOOL)isFinished {  
    return finished;  
}  
- (void)start {  
    //第一步就要檢測是否被取消了,如果取消了,要實現(xiàn)相應的KVO  
    if ([self isCancelled]) {  
        [self willChangeValueForKey:@"isFinished"];  
        finished = YES;  
        [self didChangeValueForKey:@"isFinished"];  
        return;  
    }  
    //如果沒被取消,開始執(zhí)行任務  
    [self willChangeValueForKey:@"isExecuting"];  
    [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];  
    executing = YES;  
    [self didChangeValueForKey:@"isExecuting"];  
}  
- (void)main {  
    @try {  
        @autoreleasepool {  
            //在這里定義自己的并發(fā)任務  
            NSLog(@"自定義并發(fā)操作NSOperation");  
            NSThread *thread = [NSThread currentThread];  
            NSLog(@"%@",thread);  
            //任務執(zhí)行完成后要實現(xiàn)相應的KVO  
            [self willChangeValueForKey:@"isFinished"];  
            [self willChangeValueForKey:@"isExecuting"];  
            executing = NO;  
            finished = YES;  
            [self didChangeValueForKey:@"isExecuting"];  
            [self didChangeValueForKey:@"isFinished"];  
        }  
    }     
    }   
}  
@end  

我們來測試一下:

//  ViewController.m  
//  自定義并發(fā)NSOperation  
//  
//  Created by MrZhao on 16/9/13.  
//  Copyright ? 2016年 MrZhao. All rights reserved.  
#import "ViewController.h"  
#import "ZCCurrentOperation.h"  
@interface ViewController ()  
@property (nonatomic,strong)NSOperationQueue *myQueue;  
@implementation ViewController  
- (void)viewDidLoad {  
    [super viewDidLoad];  
    self.myQueue = [[NSOperationQueue alloc] init];  
    ZCCurrentOperation *operation1 = [[ZCCurrentOperation alloc] init];  
    ZCCurrentOperation *operation2 = [[ZCCurrentOperation alloc] init];  
    [self.myQueue addOperation:operation2];  

    ZCCurrentOperation *operation3 = [[ZCCurrentOperation alloc] init];  
    [self.myQueue addOperation:operation3];  
}  
@end
20160913180613489.png

以上就是實現(xiàn)自定義NSOperation的相關內(nèi)容,下面章節(jié)會給出一個具體的應用實例。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容