美文网首页
NSOperation

NSOperation

作者: 蚂蚁牙齿不黑 | 来源:发表于2018-06-13 14:53 被阅读12次

NSOperation

  • 配合使用NSOperation和NSOperationQueue也能实现多线程编程
  • 对GCD的一层封装

基本使用

    // 1.创建队列     如果想再主线程中执行任务, 那么直接创建mainQueu即可
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // maxConcurrentOperationCount 默认等于 -1, 代表不限制, 可以创建N多线程
    // 如果想实现串行, 那么就设置 = 1
    queue.maxConcurrentOperationCount = 10;
   
    // 2.创建任务1
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务1 = %@", [NSThread currentThread]);
    }];
    //   创建任务2
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务2 = %@", [NSThread currentThread]);
    }];
    //   创建任务3
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务3 = %@", [NSThread currentThread]);
    }];
    
    // 3.将任务添加到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];

设置依赖 限制操作执行顺序

 //  创建一个并发队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    __block UIImage *image1 = nil;
    __block UIImage *image2 = nil;
    // 1.开启一个线程下载第一张图片
    NSOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531131224133&di=892ad8eaa917649a84a49d7f1d60d5e6&imgtype=0&src=http%3A%2F%2Fwww.ycen.com.cn%2Fzhuanti%2F2014yccz%2Fycczcm%2F201407%2FW020140724618688123668.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 2.生成下载好的图片
        UIImage *image = [UIImage imageWithData:data];
        image1 = image;
         NSLog(@"第一张图片下载完毕");
    }];
  // 2.开启一个线程下载第二长图片
    NSOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"http://i0.letvimg.com/lc02_yunzhuanma/201503/07/00/48/170acdff2830753f89957742b0e8f5b8_25431356/thumb/2_400_225.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        // 2.生成下载好的图片
        UIImage *image = [UIImage imageWithData:data];
        image2 = image;
         NSLog(@"第二张图片下载完毕");
        
    }];
    // 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;
        }];
    }];

    // 4. 设置依赖
    
    //  op1 执行完成才能执行  op3
    [op3 addDependency:op1];
    
    //  op2 执行完成才能执行  op3
    [op3 addDependency:op2];
   
    // 5. 将任务添加到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];

相关文章

网友评论

      本文标题:NSOperation

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