NSOperation、Queue使用注意点总结

2018-05-07  本文已影响242人  SmallWhiteMouse

NSOperation、NSOperationQueue 是苹果提供给我们的一套多线程解决方案。实际上 NSOperation、NSOperationQueue 是基于 GCD 更高一层的封装,完全面向对象。但是比 GCD 更简单易用、代码可读性也更高。

1.NSOperation基本使用

NSOperation可以调用start方法来执行任务,但默认是同步执行的
NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类(在第三条介绍子类)

2.NSOperationQueue 队列

    //创建操作(任务)
    NSOperationQueue  *queue = [[NSOperationQueue alloc]init];
    
    NSInvocationOperation  *operationInvocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoad1) object:nil];
    NSBlockOperation  *operationblock = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"downLoad3-----%@",[NSThread currentThread]);
        
    }];
    //添加任务
    [queue addOperation:operationInvocation];//自动调用 [op start]
    [queue addOperation:operationblock];//自动调用 [op start]

添加操作到NSOperationQueue中
//add后的任务不需要调用-start就会自动执行

  • (void)addOperation:(NSOperation *)op;
  • (void)addOperationWithBlock

3.NSOperation的子类

3.1 NSInvocationOperation

//如果是在主线程创建,就在主线程执行任务。如果是子线程,那就在子线程
  NSInvocationOperation  *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run) object:nil];
  [operation start];

3.2NSBlockOperation

    //包装的任务在主线程
    NSBlockOperation  *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1  --- %@",[NSThread currentThread]);
    }];
    //再添加的任务 会在新子线程执行
    [operation addExecutionBlock:^{
        NSLog(@"2 --- %@",[NSThread currentThread]);  
    }];
    [operation start];

3.3自定义NSOperation

//需要执行的任务
- (void)main{
    
    for (int i =0; i<10088; i++) {
        NSLog(@"XBSOpertion----下载任务%@",[NSThread currentThread]);
    }
    //这句写与否直接就看出你的水平
    if (self.isCancelled) return;
    
    for (int i =0; i<10088; i++) {
        NSLog(@"XBSOpertion----下载任务%@",[NSThread currentThread]);
    }
}

4.最大并发数maxConcurrentOperationCount

系统默认最大并发数是NSOperationQueueDefaultMaxConcurrentOperationCount = -1.

5.NSOperation挂起和取消

注意:
1.这里的暂停和取消(包括操作的取消和队列的取消)并不代表可以将当前的操作立即取消,而是当当前的操作执行完毕之后不再执行新的操作。
2.暂停和取消的区别就在于:暂停操作之后还可以恢复操作,继续向下执行;而取消操作之后,所有的操作就清空了,无法再接着执行剩下的操作

5.1挂起suspended.

5.2 取消

6.依赖和监听

6.1依赖[queue addExecution]

6.1.1 同队列内依赖

NSOperation之间可以设置依赖来保证执行顺序
比如一定要让操作A执行完后,才能执行操作B,可以这么写
[operationB addDependency:operationA]; // 操作B依赖于操作A

6.1.2 跨队列依赖

可以在不同queue的NSOperation之间创建依赖关系


跨队列依赖.jpeg

图解:NSOperation3依赖于NSOperation2,NSOperation2又依赖于其他队列的NSOperation6,NSOperation6又依赖于NSOperation1。

只要是个NSOperation对象就可以建立依赖,非常强大。

注意:不能相互依赖。比如A依赖B,B依赖A

6.2 监听

可以监听一个操作的执行完毕

注意点:苹果官方描述:
A finished (or canceled) operation is still given a chance to execute its completion block before it is removed from the queue

一个已经完成(或者已经被取消)的操作在被移除操作队列之前仍然有机会去执行completion block。

7.知识实践

下载两张图片,然后合成一张显示处出啦

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSOperationQueue  *queueOp = [[NSOperationQueue alloc]init];
    __block UIImage  *image1 = nil;
    
    //下载图片1
    NSBlockOperation  *downLoad1 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL  *url = [NSURL URLWithString:@"https://img.haomeiwen.com/i6687791/5ad29d1a5c1c84db.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"];

        image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        
    }];

    __block  UIImage  *image2 = nil;
    NSBlockOperation  *downLoad2 = [NSBlockOperation blockOperationWithBlock:^{
       
        NSURL  *url = [NSURL URLWithString:@"https://img.haomeiwen.com/i6687791/2a51b4b27040ed41.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"];
        
        image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    }];
 
    
    
    //4.等待两张图片下载完成,将两张图片合成一张
    
    NSBlockOperation  *combine = [NSBlockOperation blockOperationWithBlock:^{
        //开启新的图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(300, 300));
        //绘制图片
        [image1 drawInRect:CGRectMake(0, 0, 300, 150)];
        image1 = nil;
        [image2 drawInRect:CGRectMake(0, 150, 300, 150)];
        image2 = nil;
        //取得图形上下文中的图片
        UIImage  *newImage = UIGraphicsGetImageFromCurrentImageContext();
        //结束上下文
        UIGraphicsEndImageContext();
        //回到主线程显示图片
       
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = newImage;
        }];
        
        
    }];
    //combine合成任务的执行依赖 downLoad1  和 downLoad2的执行完成
    [combine addDependency:downLoad1];
    [combine addDependency:downLoad2];
    
    //三个任务添加到操作队列中
    [queueOp addOperation:downLoad1];
    [queueOp addOperation:downLoad2];
    [queueOp addOperation:combine];
    
}

如果您有什么疑问或者发现书写歧义,非常感激您能留言~

上一篇 下一篇

猜你喜欢

热点阅读