美文网首页
7-4 多线程-NSOperation

7-4 多线程-NSOperation

作者: 草根小强 | 来源:发表于2019-04-22 15:45 被阅读0次

    多线程-NSOperation

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //NSOperation 基于GCD的封装,是一个面向对象的类
        //NSOperation它是一个抽象父类  不会直接使用它 而是使用它的子类
        
        //NSInvocationOperation 是NSOperation的子类 它是以函数作为任务
        NSInvocationOperation *invo = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operationRun) object:nil];
        
        //NSBlockOperation 是NSOperation的子类  它是block作为任务
        NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"NSBlockOperation 任务执行了");
        }];
        
        //NSOperationQueue 任务队列
        NSOperationQueue *queue = [[NSOperationQueue alloc]init];
        
        //最大并发数
    //    queue.maxConcurrentOperationCount = 2;
        
        //如果最大并发数设置成1 当前队列里的线程就变成了串行的 按照顺序依次执行
    //    queue.maxConcurrentOperationCount = 1;
    //    
    //    //将任务添加到队列里
    //    [queue addOperation:invo];
    //    [queue addOperation:block];
        
        //线程的依赖关系  前面的任务依赖后面的任务  后面的任务执行完 才执行前面的任务
        [invo addDependency:block];
        //可以添加多个依赖关系
        
        //把任务添加到队列里
        [queue addOperation:invo];
        [queue addOperation:block];
        
        //如果有必要 可以取消线程
    //    [block cancel];
        
        //把队列里的任务全部取消
    //    [queue cancelAllOperations];
        
        //拿到当前队列
        [NSOperationQueue currentQueue];
        
        //拿到主线程队列
        [NSOperationQueue mainQueue];
    }
    
    - (void)operationRun{
        
        NSLog(@"NSInvocationOperation 任务执行了");
    }
    @end
    

    多线程-NSOperation

    #import "ViewController.h"
    #import "MyOperation.h"
    
    @interface ViewController ()
    
    @property (nonatomic,strong) MyOperation *operation;
    
    @property (nonatomic,strong) NSOperationQueue *queue;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.operation = [MyOperation operationOpenUrl:@"http://onepiece.local/SH/3.jpg"];
        
        self.queue = [[NSOperationQueue alloc]init];
        
        //把自定制的任务 添加到队列里
        [self.queue addOperation:self.operation];
        
        //队列的任务数量  当operationCount为0的时候 说明该队列里的线程全部执行完毕
    //    self.queue.operationCount
        //观察者KVO 实时监测operationCount的值
        [self.queue addObserver:self forKeyPath:@"operationCount" options:NSKeyValueObservingOptionNew context:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
        //当operationCount的值为0的时候 让主线程显示数据
        if ([change[@"new"] integerValue] == 0) {
            [self performSelectorOnMainThread:@selector(showImage) withObject:self waitUntilDone:NO];
        }
    }
    
    - (void)showImage{
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        
        imageView.image = self.operation.image;
        
        [self.view addSubview:imageView];
    }
    
    
    @end
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface MyOperation : NSOperation
    
    @property (nonatomic,copy) NSString *urlStr;
    
    @property (nonatomic,strong) UIImage *image;
    
    
    +(instancetype)operationOpenUrl:(NSString *)url;
    
    
    
    
    @end
    
    #import "MyOperation.h"
    
    @implementation MyOperation
    
    //自定义Operation需要重写main函数 main函数线程的入口
    - (void)main{
        //把耗时的操作写在这里
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStr]];
        //下载的图片
        self.image = [UIImage imageWithData:data];
    }
    
    +(instancetype)operationOpenUrl:(NSString *)url{
        
        MyOperation *operation = [[MyOperation alloc]init];
        operation.urlStr = url;
        
        return operation;
    }
    
    
    
    
    @end
    

    相关文章

      网友评论

          本文标题:7-4 多线程-NSOperation

          本文链接:https://www.haomeiwen.com/subject/jvmygqtx.html