简介:
NSOperation是基于GCD封装的面向对象的开启线程的方法
NSOperation是抽象类,不具有封装操作的能力,需要使用它的子类:NSInvocationOperation、NSBlockOperation、自定义的继承于NSOperation的子类
NSOperationQueue队列:
- 1.主队列 -- [NSOperationQueue mainQueue]
- 2.非主队列(其他队列)-- [[NSOperationQueue alloc]init]
开启线程的步骤:
- 1.创建队列
- 2.把任务加入队列
NSInvocationOperation、NSBlockOperation、自定义NSOperation的子类
// 使用NSInvocationOperation封装任务
NSInvocationOperation *op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run:) object:@"op"];
[op start];
// 使用NSBlockOperation封装任务
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op -- block -- %@",[NSThread currentThread]);
}];
[op start];
/* 调用start开始执行run中的操作
* 默认情况下,调用start并不会开启新线程,会在当前线程中同步执行任务
* 如果想开启线程执行必须把任务放入非主线程的队列中,才能异步执行任务
*/
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
// 在主线程中执行
NSLog(@"op -- block -- %@",[NSThread currentThread]);
}];
// 添加额外的任务(能够开启子线程,在子线程中执行),可以理解成op的子任务
[op addExecutionBlock:^{
NSLog(@"op -- execuution1 -- %zd -- %@",i,[NSThread currentThread]);
}];
[op start];
// 注意:只要NSBlockOperation封装的任务数超过1,就会异步执行
// 自定义NSOperation的子类
// 实现main函数的重写,把需要在线程中执行的操作放到main函数中
- (void)main {
NSLog(@"WMyOperation --- %@",[NSThread currentThread]);
}
NSOperationQueue队列
// 如果将NSOperation添加到NSOperationQueue队列中,系统会自动异步执行NSOperation中的操作
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run:) object:@"run"];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op2 -- block -- %@",[NSThread currentThread]);
}];
[queue addOperation:op1]; // [op1 start]
[queue addOperation:op2];
NSOperation的操作函数
最大并发数
// 最大并发数
queue.maxConcurrentOperationCount = 2;
// 最大并发数,当设置为1的时候就是串行队列
queue.maxConcurrentOperationCount = 1;
队列的取消、暂停、恢复
// 队列的取消、暂停、恢复
queue.suspended = YES; // 暂停
queue.suspended = NO; // 恢复
[queue cancelAllOperations]; // 取消队列的所有操作
[operation cancel]; // 取消某个操作
操作依赖
// 操作依赖,可以保证执行顺序,作用相当于GCD的队列组
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op1 -- block -- %@",[NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op2 -- block -- %@",[NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op3 -- block -- %@",[NSThread currentThread]);
}];
[queue addOperation:op1]; // [op1 start]
[queue addOperation:op2];
[queue addOperation:op3];
// 操作3依赖操作1、操作2
[op3 addDependency:op1];
[op3 addDependency:op2];
// 操作1和操作2执行完成才会执行操作3
操作的监听
[op1 setCompletionBlock:^{
NSLog(@"操作1执行结束 -- ");
}];
[op2 setCompletionBlock:^{
NSLog(@"操作2执行结束 -- ");
}];
[op3 setCompletionBlock:^{
NSLog(@"操作3执行结束 -- ");
}];
最后使用上面的知识写一个下载图片并合成的方法
- (void)loadAndDealImage {
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
__block UIImage *image1 = nil;
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@ -- 1",[NSThread currentThread]);
// 下载图片1
NSURL *url = [NSURL URLWithString:@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3113153671,3782273400&fm=27&gp=0.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
image1 = [UIImage imageWithData:imageData];
}];
__block UIImage *image2 = nil;
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@ -- 2",[NSThread currentThread]);
// 下载图片2
NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=949354588,677085984&fm=27&gp=0.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
image2 = [UIImage imageWithData:imageData];
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@ -- dispatch_group_notify",[NSThread currentThread]);
// 拼接图片
// 开启新的图形上下文
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
// 绘图
[image1 drawInRect:CGRectMake(0, 0, 200, 100)];
[image2 drawInRect:CGRectMake(0, 100, 200, 100)];
// 获取上下文的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
// 返回主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
image1 = nil;
image2 = nil;
self.imageView.image = image;
}];
}];
[op3 addDependency:op1];
[op3 addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
}