iOS NSOperation

时间:2023-03-09 04:34:21
iOS NSOperation

iOS NSOperation

一、简介

  • 除了,NSThread和GCD实现多线程,配合使用NSOperation和NSOperationQueue也能实现多线程编程

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

  • 1、先将需要执行的操作封装到一个NSOperation的子类对象中2、然后将NSOperation对象添加到NSOperationQueue中
    • 实际上,NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类
  • 3、系统会自动将NSOperationQueue中的NSOperation取出来
  • 4、将取出的NSOperation封装的操作放到一条新线程中执行

二、NSOperation

  • 如上所示:要实现多线程,必须要将执行的操作封装到NSOperation的子类对象中,那么NSOperation的子类有哪些?
1、使用NSOperation子类的方式有3种
  • NSInvocationOperation
  • NSBlockOperation
  • 自定义子类继承NSOperation,实现内部相应main的方法封装操作

1.1 NSInvocationOperation

  • 创建NSInvocationOperation对象
-(id)initWithTarget:(id)target selector:(SEL)selector object:(id)arg;
  • 调用start方法开始执行操作

    • 默认情况下,调用了start方法后并不会开一条新线程去执行操作,而是在当前线程同步执行操作
      // 一旦执行操作,就会调用target的selector方法
      -(void)start;
      • 只有将NSOperation操作任务放到一个NSOperationQueue中,才会异步执行操作
  • 使用

- (void)invocation
{
// 1.将操作封装到Operation中
NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(demo) object:nil];
// 2.执行封装的操作
// 如果直接执行NSInvocationOperation中的操作, 那么默认会在主线程中执行
[op1 start]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil];
[op2 start];
}

1.2 NSBlockOperation

  • 注意点:只要NSBlockOperation封装的操作数 >1,就会异步执行操作

  • 创建NSBlockOperation对象

    +(id)blockOperationWithBlock:(void(^)(void))block;
  • 通过addExecutionBlock:方法添加更多的操作

    -(void)addExecutionBlock:(void(^)(void))block;
  • 使用

- (void)blockOperation
{
// 1.封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1- %@", [NSThread currentThread]);
}]; // 2.添加操作
[op1 addExecutionBlock:^{
NSLog(@"2- %@", [NSThread currentThread]);
}];
[op1 addExecutionBlock:^{
NSLog(@"3- %@", [NSThread currentThread]);
}]; // 2.执行操作
// 如果只封装了一个操作, 那么默认会在主线程中执行
// 如果封装了多个操作, 那么除了第一个操作以外, 其它的操作会在子线程中执行
[op1 start];
}

1.3 自定义 NSOperation,继承NSOperation

  • 如果是自定义类继承于NSOperation, 那么需要将操作写到自定义类的main方法中,重写main方法

    • 重写-(void)main方法的注意点
      • 自己创建自动释放池(因为如果是异步操作,无法访问主线程的自动释放池)
      • 经常通过-(BOOL)isCancelled方法检测操作是否被取消,对取消做出响应
  • 这种实现方式封装操作, 可以提高代码的复用性

  • 1.创建类JPOperation,继承NSOperation

#import <Foundation/Foundation.h>
@interface JPOperation : NSOperation @end #import "JPOperation.h" @implementation JPOperation // 我们要重写main方法,封装操作
- (void)main
{
NSLog(@"%s, %@", __func__,[NSThread currentThread]);
}
@end
  • 2.使用自定义的NSOperation
    // 1.封装操作
JPOperation *op1 = [[JPOperation alloc] init];
// 2.执行操作
[op1 start]; JPOperation *op2 = [[JPOperation alloc] init];
[op2 start];

三、NSOperationQueue

  • NSOperationQueue的作用:

    • 如上所述:NSOperation可以调用start方法来执行任务,但默认是同步执行的
    • 如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
  • 添加操作到NSOperationQueue:两个方法

    • 只要将一个任务添加到alloc/init的队列(默认并发,可以设置其为串行)中, 那么队列内部会自动调用start
    • 如果想实现串行, 那么就设置队列的maxConcurrentOperationCount = 1
-(void)addOperation:(NSOperation*)op;
-(void)addOperationWithBlock:(void(^)(void))block;

基本使用

  • GCD队列:

    • 串行: 自己创建, 主队列
    • 并发: 自己创建, 全局
  • NSOperationQueue:

    • 自己创建: alloc/init --> 默认是并发 --> 也可以让它串行
    • 主队列 : mainQueue
#import "ViewController.h"
#import "JPOperation.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 2.封装任务
JPOperation *op1 = [[JPOperation alloc] init];
JPOperation *op2 = [[JPOperation alloc] init]; // 3.将任务添加到队列中
[queue addOperation:op1];
[queue addOperation:op2];
} - (void)block
{
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 2.将任务添加到队列中
// addOperationWithBlock方法会做两件事情
// 1.根据传入的block, 创建一个NSBlockOperation对象
// 2.将内部创建好的NSBlockOperation对象, 添加到队列中
[queue addOperationWithBlock:^{
NSLog(@"1 = %@", [NSThread currentThread]);
}];
[queue addOperationWithBlock:^{
NSLog(@"2 = %@", [NSThread currentThread]);
}];
} - (void)invation
{
/*
GCD队列:
串行: 自己创建, 主队列
并发: 自己创建, 全局 NSOperationQueue:
自己创建: alloc/init --> 默认是并发 --> 也可以让它串行
主队列 : mainQueue
*/
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// NSOperationQueue *queue = [NSOperationQueue mainQueue]; // 2.封装任务
NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(demo) object:nil];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil]; // 3.将任务添加到队列中
// 只要将一个任务添加到alloc/init的队列中, 那么队列内部会自动调用start
// 只要将一个任务添加到alloc/init的队列中, 就会开启一个新的线程执行队列
[queue addOperation:op1];
[queue addOperation:op2];
} - (void)demo
{
NSLog(@"demo = %@", [NSThread currentThread]);
}
- (void)test
{
NSLog(@"test = %@", [NSThread currentThread]);
}
@end

四、NSOperatinoQueue的串行和并发 : 最大并发数

  • 队列的maxConcurrentOperationCount最大并发数

  • maxConcurrentOperationCount 默认等于 -1, 代表不限制, 可以创建N多线程

  • alloc/init的NSOperatinoQueue队列默认就是并发, 如果想实现串行, 那么就设置maxConcurrentOperationCount = 1
  • 注意: 最大并发数, 不能设置为0, 否则任务不会被执行
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init
];
// maxConcurrentOperationCount 默认等于 -1, 代表不限制, 可以创建N多线程
// 默认就是并发
// 如果想实现串行, 那么就设置maxConcurrentOperationCount = 1
// 注意: 最大并发数, 不能设置为0, 否则任务不会被执行 \
如果想再主线程中执行任务, 那么直接创建mainQueu即可
// queue.maxConcurrentOperationCount = 1; // 2.创建任务
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"1 = %@", [NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"2 = %@", [NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1];
NSLog(@"3 = %@", [NSThread currentThread]);
}]; // 3.将任务添加到队列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
}
@end

五、NSOperationQueue的暂停-恢复-取消

1、取消队列的所有操作

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

  • 注意点:

    • 任务只要被取消, 就不会再恢复了
    • 取消任务和暂停任务一样, 不会取消当前正在执行的任务, 只能取消还未执行的任务
    -(void)cancelAllOperations;

2、暂停和恢复队列

  • 注意:

    • 1.如果在任务执行的过程中暂停队列中的任务, 那么当前正在执行的任务并不会被暂停, 而是会暂停队列中的下一个任务
    • 2.恢复任务, 是从队列第一个没有被执行过的任务开始恢复
    -(void)setSuspended:(BOOL)b;//如果是YES, 代表需要暂停,NO代表代表不需要暂停 ==  恢复执行
    -(BOOL)isSuspended;

六、NSOperationQueue线程间通信

实例程序:开启子线程下载图片,下载好图片后,回到主线程进行更新UI

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.开启子线程下载图片
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// 子线程
NSString *urlStr = @"https://www.baidu.com/img/bd_logo1.png";
// url中文编码,防止乱码
// urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下载好的图片
UIImage *image = [UIImage imageWithData:data]; // 3.回到主线程更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"更新UI");
// 主线程
self.imageView.image = image;
}];
}];
}
@end

七、操作依赖

  • 1、目的 -> NSOperation之间可以设置依赖来保证执行顺序
    • 例如:一定要让操作A执行完后,才能执行操作B,可以这么写
  • 只要添加了依赖, 那么就会等依赖的任务执行完毕, 才会执行当前任务

    [operationB addDependency:operationA];// 操作B依赖于操作A , A操作执行完才会执行操作B
  • 2、除了同一quere操作间建立依赖关系,当然也可以在不同queue的NSOperation之间创建依赖关系

  • 注意点:

    • 不能相互依赖 -> 比如A依赖B,B依赖A

经典实例:合成图片

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSOperationQueue *queue2 = [[NSOperationQueue alloc] init]; __block UIImage *image1 = nil;
__block UIImage *image2 = nil;
// 1.开启一个线程下载第一张图片
NSOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://cdn.cocimg.com/assets/images/logo.png?v=201510272"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下载好的图片
UIImage *image = [UIImage imageWithData:data];
image1 = image;
}]; // 2.开启一个线程下载第二长图片
NSOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/img/bd_logo1.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
// 2.生成下载好的图片
UIImage *image = [UIImage imageWithData:data];
image2 = image; }];
// 3.开启一个线程合成图片
NSOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[image1 drawInRect:CGRectMake(0, 0, 100, 200)];
[image2 drawInRect:CGRectMake(100, 0, 100, 200)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // 4.回到主线程更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"回到主线程更新UI");
self.imageView.image = newImage;
}];
}]; // 监听任务是否执行完毕
op1.completionBlock = ^{
NSLog(@"第一张图片下载完毕");
};
op2.completionBlock = ^{
NSLog(@"第二张图片下载完毕");
}; // 添加依赖
// 只要添加了依赖, 那么就会等依赖的任务执行完毕, 才会执行当前任务
// 注意:
// 1.添加依赖, 不能添加循环依赖
// 2.NSOperation可以跨队列添加依赖
[op3 addDependency:op1];
[op3 addDependency:op2]; // 将任务添加到队列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue2 addOperation:op3];
}
@end