概述

二 NSOperation中的几个概念

2.1 NSOperation说明

NSOperation的作用

配合使用NSOperation和NSOperationQueue也能实现多线程编程

NSOperation和NSOperationQueue实现多线程的具体步骤

2.2 NSOperation的子类

NSOperation是个抽象类,并不具备封装操作能力,必须使用它的子类使用NSOperation子嘞的方式有3种:

2.3 NSOperationQueue

NSOperationQueue的作用
添加操作到NSOperationQueue中
-(void)addOperation:(NSOperation *)op;
-(void)addOperationWithBlock:(void (^)(void))block;

2.4 最大并发

什么最大并发
最大并发数的相关方法
-(NSInteger)maxConcurrentOperationCount;
-(void)setMaxConcurrentOperationCount:(NSInteger)cnt;

2.5 操作依赖

NSOperation之间可以设置依赖来保证执行顺序
比如一定要让操作A执行完后,才能执行操作B,可以这么写

[operationB addDependency:operationA];//操作B依赖于操作A

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

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

2.6 操作的监听

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

-(void)(^)(void)completionBlock;
-(void)setCompletionBlock:(void)(^)(void)block;

2.7 队列取消、暂停、恢复

取消队列的所有操作
-(void)cancelAllOperations;

提示:也可以调用NSOperation的-(void)cancel方法取消单个操作

暂停和恢复队列
-(void)setSuspended:(Bool)b;YES代表暂停队列,NO代表恢复队列
-(BOOL)isSuspended;

2.8 自定义NSOperation

自定义NSOperation的步骤简单

重写-(void)main方法,在里面实现想执行的任务

重写-(void)main方法的注意点
定义NSOperation下载图片思路

三 NSOperation示例

3.1 NSInvocationOperation

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];

    //operation直接调用start,是同步执行(在当前线程)
    NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
    //[operation start];
    //添加操作到队列中,会自动异步执行
    [queue addOperation:operation];
    
}
-(void)download{
    NSLog(@"download......%@",[NSThread currentThread]);
}
@end

3.2 NSBlockOperation示例

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSBlockOperation *operation1=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation4=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    }];
    //创建队列
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    //主队列
    //NSOperationQueue *queue=[NSOperationQueue mainQueue];
    
    //添加操作到队列中(自动异步执行)
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
}
-(void)test{
    //    NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
    //        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    //    }];
    
    NSBlockOperation *operation=[[NSBlockOperation alloc]init];
    [operation addExecutionBlock:^{
        NSLog(@"---下载图片--1-%@",[NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"---下载图片--2-%@",[NSThread currentThread]);
    }];
    [operation addExecutionBlock:^{
        NSLog(@"---下载图片--3-%@",[NSThread currentThread]);
    }];
    [operation start];
    //任务数量>1,才会开始异步执行。
}
@end

3.3 NSOperationQueue



@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    [queue addOperationWithBlock:^{
        //1-异步下载图片
        NSURL *url = [NSURL URLWithString:@"https://img0.baidu.com/it/u=1929941019,3324507395&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        //2-回到主线程,显示图片
        //[self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:dispath];
        //dispatch_async(dispatch_main(), <#^(void)block#>)
        
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                    self.imageView.image=image;
                }];
        
    }];
    [queue cancelAllOperations];//取消列中的所有任务(不可恢复)
    [queue setSuspended:YES];//暂停队列中的所有任务
    [queue setSuspended:NO];//恢复队列中的所有任务
    
}

-(void)addOperation3{
    //假设有A、B、C三个操作,要求:
    //1.3个操作都异步执行
    //2.操作C依赖于操作B
    //3.操作B依赖于操作A
    
    //1.创建一个队列(非主队列)
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    //2.创建3个操作
    NSBlockOperation *operationA=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"A1----%@",[NSThread currentThread]);
    }];
//    [operationA addExecutionBlock:^{
//        NSLog(@"A2----%@",[NSThread currentThread]);
//        }];
//    [operationA setCompletionBlock:^{
//        NSLog(@"AAA----%@",[NSThread currentThread]);
//    }];
    NSBlockOperation *operationB=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"B----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operationC=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"C----%@",[NSThread currentThread]);
    }];
    
    //设置依赖
    [operationB addDependency:operationA];
    [operationC addDependency:operationB];
    
    //添加操作到队列中(自动异步执行)
    [queue addOperation:operationA];
    [queue addOperation:operationB];
    [queue addOperation:operationC];
}
-(void)addOperationDepend{
    //假设有A、B、C三个操作,要求:
    //1.3个操作都异步执行
    //2.操作C依赖于操作B
    //3.操作B依赖于操作A
    
    //1.创建一个队列(非主队列)
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    //2.创建3个操作
    NSBlockOperation *operationA=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"A----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operationB=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"B----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operationC=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"C----%@",[NSThread currentThread]);
    }];
    
    //设置依赖
    [operationB addDependency:operationA];
    [operationC addDependency:operationB];
    
    //添加操作到队列中(自动异步执行)
    [queue addOperation:operationA];
    [queue addOperation:operationB];
    [queue addOperation:operationC];
    
}
-(void)maxcount{
    //1-创建一个队列
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    
    //2-设置最大并发数
    [queue setMaxConcurrentOperationCount:2];
    
    //3添加操作队列中(自动异步执行任务,并发)
    NSBlockOperation *operation1=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片2----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片3----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation4=[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片4----%@",[NSThread currentThread]);
    }];
    
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
}
-(void)baseUse{
    //1-创建一个队列
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    
    //2添加操作队列中(自动异步执行任务,并发)
//    NSBlockOperation *operation1=[NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"下载图片1----%@",[NSThread currentThread]);
//    }];
//    NSBlockOperation *operation2=[NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"下载图片2----%@",[NSThread currentThread]);
//    }];
    
    //[queue addOperation:operation1];
    //[queue addOperation:operation2];
    
    [queue addOperationWithBlock:^{
        NSLog(@"下载图片3----%@",[NSThread currentThread]);
    }];
}
@end

原文地址:https://blog.csdn.net/Calvin_zhou/article/details/123162265

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_38738.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注