iOS 学习笔记 --- 多线程基础(4)--- NSOperation

NSOperation 简介

  • 实现多线程步骤

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

  • 使用NSOperation子类的方式有3种
    • NSInvocationOperation
    • NSBlockOperation
    • 自定义子类继承NSOperation,实现内部相应的方法

具体使用

  • NSInvocationOperation
- (void)invocationOperation
{
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];

    [op start];
}

- (void)run
{
    NSLog(@"------%@", [NSThread currentThread]);
}
  • NSBlockOperation
- (void)blockOperation
{
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        // 在主线程
        NSLog(@"下载1------%@", [NSThread currentThread]);
    }];

    // 添加额外的任务(在子线程执行)
    [op addExecutionBlock:^{
        NSLog(@"下载2------%@", [NSThread currentThread]);
    }];

    [op addExecutionBlock:^{
        NSLog(@"下载3------%@", [NSThread currentThread]);
    }];
    [op addExecutionBlock:^{
        NSLog(@"下载4------%@", [NSThread currentThread]);
    }];

    [op start];
}
  • NSOperationQueue

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

    • 主队列
      • [NSOperationQueue mainQueue]
      • 凡是添加到主队列中的任务(NSOperation),都会放到主线程中执行
    • 非主队列(其他队列)
      • [[NSOperationQueue alloc] init]
      • 同时包含了:串行、并发功能
      • 添加到这种队列中的任务(NSOperation),就会自动放到子线程中执行
- (void)operationQueue1
{
    // 创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // 创建操作(任务)
    // 创建NSInvocationOperation
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download1) object:nil];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download2) object:nil];

    // 创建NSBlockOperation
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download3 --- %@", [NSThread currentThread]);
    }];

    [op3 addExecutionBlock:^{
        NSLog(@"download4 --- %@", [NSThread currentThread]);
    }];
    [op3 addExecutionBlock:^{
        NSLog(@"download5 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"download1 --- %@", [NSThread currentThread]);
    }];
    [queue addOperationWithBlock:^{
        NSLog(@"download2 --- %@", [NSThread currentThread]);
    }];
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download6 --- %@", [NSThread currentThread]);
    }];

    // 创建XMGOperation
    XMGOperation *op5 = [[XMGOperation alloc] init];

    // 添加任务到队列中
    [queue addOperation:op1]; // [op1 start]
    [queue addOperation:op2]; // [op2 start]
    [queue addOperation:op3]; // [op3 start]
    [queue addOperation:op4]; // [op4 start]
    [queue addOperation:op5]; // [op5 start]
}

- (void)download1
{
    NSLog(@"download1 --- %@", [NSThread currentThread]);
}

- (void)download2
{
    NSLog(@"download2 --- %@", [NSThread currentThread]);
}
  • 自定义Operation
//.h文件中
#import <Foundation/Foundation.h>
//继承自 NSOperation
@interface XMGOperation : NSOperation

//.m文件中 重写main方法
/**
 * 需要执行的任务 
 */
- (void)main
{
    for (NSInteger i = 0; i<1000; i++) {
        NSLog(@"download1 -%zd-- %@", i, [NSThread currentThread]);
    }
    //判断是否取消了任务
    if (self.isCancelled) return;
}

//引用位置
XMGOperation *op5 = [[XMGOperation alloc] init];
[queue addOperation:op5]; // [op5 start]
@end
  • 最大并发数
    • 默认为 -1 不限制并发数
// 设置最大并发操作数
    //    queue.maxConcurrentOperationCount = 2;
    queue.maxConcurrentOperationCount = 1; // 就变成了串行队列
  • 队列的取消、暂停、恢复
 if (self.queue.isSuspended) {
        // 恢复队列,继续执行
        self.queue.suspended = NO;
    } else {
        // 暂停(挂起)队列,暂停执行
        self.queue.suspended = YES;
    }
    //取消所有操作
    [self.queue cancelAllOperations];
  • 操作依赖

    • [operationB addDependency:operationA]; // 操作B依赖于操作A
    • 添加之前设置
    • 可以跨队列依赖
  • 操作的监听

NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download5----%@", [NSThread  currentThread]);
    }];
    op5.completionBlock = ^{
        NSLog(@"op5执行完毕---%@", [NSThread currentThread]);
    };
  • 线程间的通讯
/**
 * 线程之间的通信
 */
- (void)test
{
    [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
        // 图片的网络路径
       NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        // 加载图片
        NSData *data = [NSData dataWithContentsOfURL:url];

        // 生成图片
        UIImage *image = [UIImage imageWithData:data];

        // 回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
    }];
}
  • 注:资料多是自己网上摘抄整理的,记录下来以便学习查询之用,严格原创出处不便考证以注明,若有侵权实属无意,敬请见谅!
    不严谨! 条件所限,很多知识点现在还吃不透,日后发现错误再做修改。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值