NSOperation是一個抽象類。實際開發(fā)中用它的兩個子類:NSInvocationOperation NSBlockOperation
//NSBlockOperation可以通過block異步執(zhí)行任務(wù),且block里面的代碼是同步的- (void)blockOperationTest{ NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"1在第%@個線程",[NSThread currentThread]); NSLog(@"1haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"2在第%@個線程",[NSThread currentThread]); NSLog(@"2haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"3在第%@個線程",[NSThread currentThread]); NSLog(@"3haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"4在第%@個線程",[NSThread currentThread]); NSLog(@"4haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"5在第%@個線程",[NSThread currentThread]); NSLog(@"5haha"); }]; [blockOperation addExecutionBlock:^{ NSLog(@"6在第%@個線程",[NSThread currentThread]); NSLog(@"6haha"); }];// [blockOperation start]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:blockOperation];}//不放大queue里面在主線程同步執(zhí)行NSInvocationOperation 類似于button添加點擊事件
- (void)invocationTest{ NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testNSOperation) object:nil]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:invo]; //不放大queue里面在主線程同步執(zhí)行}添加依賴關(guān)系是Queue的最大特征
- (void)relationTest{ NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation1) object:nil]; op1.completionBlock = ^(){ }; NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation2) object:nil]; op2.completionBlock = ^(){ }; NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation3) object:nil]; NSInvocationOperation *op4 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testWithOperation4) object:nil]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [op2 addDependency:op1]; // 操作2依賴于操作1 [op3 addDependency:op2]; [op4 addDependency:op3]; [queue addOperation:op4]; [queue addOperation:op3]; [queue addOperation:op2]; [queue addOperation:op1];}ios 針對http請求失效:https://segmentfault.com/a/1190000002933776 繼承NSOperation 的圖片請求operation類,只需要重新寫main 方法
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@PRotocol SDDownloadOperationDelegate <NSObject>- (void)downloadFinishWithImage:(UIImage *)image;@end@interface SDDownloadOperation : NSOperation@property (nonatomic, copy) NSString *imageURL;@property (nonatomic, weak) id <SDDownloadOperationDelegate> delegate;- (instancetype)initWithUrl:(NSString *)url delegate:(id<SDDownloadOperationDelegate>)delegate;@end#import "SDDownloadOperation.h"@implementation SDDownloadOperation- (instancetype)initWithUrl:(NSString *)url delegate:(id<SDDownloadOperationDelegate>)delegate{ self = [super init]; if (!self) { return nil; } self.imageURL = url; self.delegate = delegate; return self;}- (void)main{ if (self.isCancelled) { return; } NSURL *url = [NSURL URLWithString:self.imageURL]; NSData *imageData = [NSData dataWithContentsOfURL:url]; if (self.isCancelled) { url = nil; imageData = nil; return; } UIImage *image = [UIImage imageWithData:imageData]; if (self.isCancelled) { imageData = nil; return; } if (self.delegate && [self.delegate respondsToSelector:@selector(downloadFinishWithImage:)]) { [self.delegate downloadFinishWithImage:image]; }}@end新聞熱點
疑難解答