美文网首页
iOS多线程详解(三)NSOperation

iOS多线程详解(三)NSOperation

作者: 独角兽ios | 来源:发表于2016-08-03 22:02 被阅读53次

基于GCD的一层封装,也是面向队列和任务来开发,比GCD更加面向对象

NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类

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

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

GCD 和 NSOperation 的队列对比

GCD的队列类型

  • 并发队列

    • 自己创建的
    • 全局
  • 串行队列

    • 主队列
    • 自己创建的
    // 1.获得全局的并发队列
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
      
      // 2.将任务加入队列
      dispatch_async(queue, ^{
          for (NSInteger i = 0; i<10; i++) {
              NSLog(@"1-----%@", [NSThread currentThread]);
          }
      });
     // 将任务加入队列
      dispatch_async(queue, ^{
          for (NSInteger i = 0; i<10; i++) {
              NSLog(@"2-----%@", [NSThread currentThread]);
          }
      });
      // 将任务加入队列
      dispatch_async(queue, ^{
          for (NSInteger i = 0; i<10; i++) {
              NSLog(@"3-----%@", [NSThread currentThread]);
          }
      });
    
>#### NSOperationQueue的队列类型
- 主队列
   - [NSOperationQueue mainQueue]
   - 凡是添加到主队列中的任务(NSOperation),都会放到主线程中执行
- 非主队列(其他队列)
   - [[NSOperationQueue alloc] init]
   - 同时包含了:串行、并发功能
   - 添加到这种队列中的任务(NSOperation),就会自动放到子线程中执行

// 创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

// 设置最大并发操作数
queue.maxConcurrentOperationCount = 2; 并行队列
// queue.maxConcurrentOperationCount = 1; // 就变成了串行队列

// 添加操作到队列中
//    [queue addOperation:op1];
[queue addOperationWithBlock:^{
    NSLog(@"download1 --- %@", [NSThread currentThread]);
}];
[queue addOperationWithBlock:^{
    NSLog(@"download2 --- %@", [NSThread currentThread]);
}];
##线程间通信
-1.简单例子( 和GCD很像)

[[[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;
    }];
}];

}

-2.简单模拟下载图片并合成图片(添加依赖关系,控制线程的执行顺序)

__block UIImage *image1 = nil;
// 下载图片1
NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{

    // 图片的网络路径
    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];
    
    // 生成图片
    image1 = [UIImage imageWithData:data];
}];

__block UIImage *image2 = nil;
// 下载图片2
NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
    
    // 图片的网络路径
    NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];

    
    // 加载图片
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    // 生成图片
    image2 = [UIImage imageWithData:data];
}];

// 合成图片
NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
    // 开启新的图形上下文
    UIGraphicsBeginImageContext(CGSizeMake(100, 100));
    
    // 绘制图片
    [image1 drawInRect:CGRectMake(0, 0, 50, 100)];
    image1 = nil;
    
    [image2 drawInRect:CGRectMake(50, 0, 50, 100)];
    image2 = nil;
    
    // 取得上下文中的图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 结束上下文
    UIGraphicsEndImageContext();
    
    // 回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        self.imageView.image = image;
    }];
}];
[combine addDependency:download1];
[combine addDependency:download2];

[queue addOperation:download1];
[queue addOperation:download2];
[queue addOperation:combine];
 - 补充


![Snip20160723_3.png](https://img.haomeiwen.com/i1777166/98ea5243b9ddabe4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:iOS多线程详解(三)NSOperation

      本文链接:https://www.haomeiwen.com/subject/kkqwjttx.html