Operation Queues 在GCD之前就有了暴备,其中的某些設(shè)計原理因Operation Queues而流行,GCD就是基于這些原理構(gòu)建的暑竟。從iOS4與Mac OSX 10.6開始,Operation Queues在底層就是用GCD來實現(xiàn)的。
GCD是純C的API,而Operation Queues是Objective-C的對象。
- Operation Queues :相對 GCD 來說,使用 Operation Queues 會增加一點點額外的開銷喧笔,但是我們卻換來了非常強大的靈活性和功能,我們可以給 operation 之間添加依賴關(guān)系龟再、取消一個正在執(zhí)行的 operation 书闸、暫停和恢復 operation queue 等;
- GCD :則是一種更輕量級的利凑,以 FIFO 的順序執(zhí)行并發(fā)任務(wù)的方式浆劲,使用 GCD 時我們并不關(guān)心任務(wù)的調(diào)度情況嫌术,而讓系統(tǒng)幫我們自動處理。但是 GCD 的短板也是非常明顯的牌借,比如我們想要給任務(wù)之間添加依賴關(guān)系度气、取消或者暫停一個正在執(zhí)行的任務(wù)時就會變得非常棘手。
NSOperation
很多執(zhí)行任務(wù)類型的案例都很好的運用了NSOperation
膨报,包括網(wǎng)絡(luò)請求磷籍,圖像壓縮,自然語言處理或者其他很多需要返回處理后數(shù)據(jù)的现柠、可重復的院领、結(jié)構(gòu)化的、相對長時間運行的任務(wù)
比如我們經(jīng)常使用的AFNetworking
@interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSSecureCoding, NSCopying>
@property (nonatomic, strong) NSSet *runLoopModes;
@property (readonly, nonatomic, strong) NSURLRequest *request;
@property (readonly, nonatomic, strong) NSURLResponse *response;
@property (readonly, nonatomic, strong) NSError *error;
@property (readonly, nonatomic, strong) NSData *responseData;
@property (readonly, nonatomic, copy) NSString *responseString;
...
SDWebImage
extern NSString *const SDWebImageDownloadStartNotification;
extern NSString *const SDWebImageDownloadReceiveResponseNotification;
extern NSString *const SDWebImageDownloadStopNotification;
extern NSString *const SDWebImageDownloadFinishNotification;
繼承自NSOperation
@interface SDWebImageDownloaderOperation : NSOperation <SDWebImageOperation>
@property (strong, nonatomic, readonly) NSURLRequest *request;
@property (assign, nonatomic) BOOL shouldDecompressImages;
...
- (id)initWithRequest:(NSURLRequest *)request
options:(SDWebImageDownloaderOptions)options
progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageDownloaderCompletedBlock)completedBlock
cancelled:(SDWebImageNoParamsBlock)cancelBlock;
@end
子類化NSOperation有兩種類型
- 非并發(fā)型NSOperation
子類化一個非并發(fā)型operation非常簡單晒旅,只需要重寫main方法即可栅盲。其他的一些復雜的操作比如工程配置,KVO通知父類已經(jīng)幫我們做好了废恋。
main方法執(zhí)行結(jié)束,該類就被釋放了扒寄。 - 并發(fā)型NSOperation
至少需要實現(xiàn)以下幾個方法
- (void)start;
- (BOOL)isConcurrent;
- (BOOL)isExecuting;
- (BOOL)isFinished;
以AFNetworking為例
@implementation AFURLConnectionOperation
- (instancetype)initWithRequest:(NSURLRequest *)urlRequest {
NSParameterAssert(urlRequest);
self = [super init];
if (!self) {
return nil;
}
_state = AFOperationReadyState;
self.lock = [[NSRecursiveLock alloc] init];
self.lock.name = kAFNetworkingLockName;
self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
self.request = urlRequest;
self.shouldUseCredentialStorage = YES;
self.securityPolicy = [AFSecurityPolicy defaultPolicy];
return self;
}
- (void)start {
[self.lock lock];
if ([self isCancelled]) {
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
} else if ([self isReady]) {
self.state = AFOperationExecutingState;
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
}
[self.lock unlock];
}
- (BOOL)isConcurrent {
return YES;
}
- (BOOL)isExecuting {
return self.state == AFOperationExecutingState;
}
- (BOOL)isFinished {
return self.state == AFOperationFinishedState;
}
發(fā)出isFinished的kvo通知鱼鼓,代表著opration結(jié)束
typedef NS_ENUM(NSInteger,MyOperationState) {
MyOperationStateReady,
MyOperationStateExecuting,
MyOperationStateFinished,
};
@interface MyOperation ()
@property (nonatomic, copy) MyOperationAction action;
@property (nonatomic, assign) MyOperationState state;
@property (nonatomic, readonly,getter=isCancelled) BOOL cancel;
@end
@implementation MyOperation
#pragma mark - Override
- (BOOL)isReady{
return self.state == MyOperationStateReady;
}
- (BOOL)isConcurrent{
return YES;
}
- (BOOL)isExecuting{
return self.state == MyOperationStateExecuting;
}
- (BOOL)isFinished{
return self.state == MyOperationStateFinished;
}
- (void)cancel{
[self willChangeValueForKey:@"isCancelled"];
_cancel = YES;
[self didChangeValueForKey:@"isCancelled"];
}
//在start中建立一個thread執(zhí)行任務(wù)
- (void)start{
if([self isReady]){
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isReady"];
self.state = MyOperationStateExecuting;
[self didChangeValueForKey:@"isReady"];
[self didChangeValueForKey:@"isExecuting"];
[self performSelector:@selector(operationDidStart) onThread:[[self class] threadForMyOperation] withObject:nil waitUntilDone:NO];
}
}
- (void)operationDidStart{
if (self.isCancelled) {
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isCancelled"];
self.state = MyOperationStateFinished;
[self didChangeValueForKey:@"isCancelled"];
[self didChangeValueForKey:@"isFinished"];
}else{
self.action();
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
self.state = MyOperationStateFinished;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
}
#pragma mark Create Singleton Thread
+ (void)keepThreadAlive{
do{
@autoreleasepool {
[[NSRunLoop currentRunLoop] run];
}
}while (YES);
}
+ (NSThread*)threadForMyOperation{
static NSThread *_threadInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_threadInstance = [[NSThread alloc] initWithTarget:self selector:@selector(keepThreadAlive) object:nil];
_threadInstance.name = @"MyOperation.Thread";
[_threadInstance start];
});
return _threadInstance;
}
@end
NSOPerationQueue
但是僅僅把計算封裝進一個對象而不做其他處理顯然沒有多大用處,我們還需要NSOperationQueue來大顯身手该编。當operation被添加到隊列之后迄本,NSOperationQueue會瀏覽所有的operation,優(yōu)先運行那些處于ready狀態(tài)且優(yōu)先級較高的操作课竣。
http://nshipster.cn/nsoperation/
//創(chuàng)建隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//將NSOperation加入隊列之后嘉赎,queue會自動執(zhí)行該operation
[queue addOperation:operation];
//設(shè)置并發(fā)數(shù)目為2
queue.maxConcurrentOperationCount = 2;
取消任務(wù)
// 取消一個任務(wù)
[operation cancel];
// 取消queue中所有的任務(wù)
[queue cancelAllOperations];
(http://nshipster.cn/nsoperation/)
http://blog.leichunfeng.com/blog/2015/07/29/ios-concurrency-programming-operation-queues/
refrence