NSOPeration NSOperationQueue

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值