NSOperation是一个抽象类。实际开发中用它的两个子类:NSInvocationOperation NSBlockOperation
//NSBlockOperation可以通过block异步执行任务,且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里面在主线程同步执行
NSInvocationOperation 类似于button添加点击事件
- (void)invocationTest{
NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testNSOperation) object:nil];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:invo];
//不放大queue里面在主线程同步执行
}
添加依赖关系是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