iOS多線程開發(fā)中碎节,NSOperation是我們經常使用的捧搞,有時系統(tǒng)自帶的兩個類NSInvocationOperation和NSBlockOperation不能滿足我們的需求時就需要我們自定義。
自定義NSOperation分兩種狮荔,一種是自定義非并發(fā)的NSOperation胎撇,一種是定義并發(fā)的NSOperation的。下面分別介紹殖氏。
一:定義非并發(fā)的NSOperation晚树。
定義非并發(fā)的NSOperation的比較簡單:
1.實現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.實現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函數中使用我們自定義的operation,并調用start方法
(void)viewDidLoad {
[super viewDidLoad];
ZCNoCurrentOperation *operation1 = [[ZCNoCurrentOperation alloc] init];
//默認情況下宝鼓,該operati
[operation1 start];
}
打印結果如下:
默認情況下,該operation在當前調用start的線程中執(zhí)行.其實如果我們創(chuàng)建多個自定義的ZCNoCurrentOperation巴刻,并放入NSOperationQueue中愚铡,這些任務也是并發(fā)執(zhí)行的,只不過因為我們沒有處理并發(fā)情況下胡陪,線程執(zhí)行完沥寥,KVO等操作,因此不建議在只實現main函數的情況下將其加入NSOperationQueue柠座,只實現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
二:自定義并發(fā)的NSOperation
自定義并發(fā)的NSOperation需要以下步驟:
1.start方法:該方法必須實現,
2.main:該方法可選妈经,如果你在start方法中定義了你的任務蒂阱,則這個方法就可以不實現锻全,但通常為了代碼邏輯清晰,通常會在該方法中定 義自己的任務
3.isExecuting isFinished 主要作用是在線程狀態(tài)改變時录煤,產生適當的KVO通知
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方法:該方法必須實現鳄厌,
2.main:該方法可選,如果你在start方法中定義了你的任務妈踊,則這個方法就可以不實現了嚎,但通常為了代碼邏輯清晰,通常會在該方法中定義自己的任務
3.isExecuting isFinished 主要作用是在線程狀態(tài)改變時廊营,產生適當的KVO通知
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 {
//第一步就要檢測是否被取消了歪泳,如果取消了,要實現相應的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í)行完成后要實現相應的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
以上就是實現自定義NSOperation的相關內容呐伞,下面章節(jié)會給出一個具體的應用實例。