NSOperation(Queue) 教程

1.NSOperation

NSInvocationOperation

- (void)invocationOperation{
    NSInvocationOperation *invOpt = [[NSInvocationOperation alloc] initWithTarget:self
                                                                         selector:@selector(run)
                                                                           object:nil];
    [invOpt start];
}
//2016-08-09 18:47:04.620 NSOperation[48338:755464] run-----<NSThread: 0x7fe1f95070c0>{number = 1, name = main}

NSBlockOperation

- (void)blockOperation1{
    NSBlockOperation *blockOpt = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载1------%@", [NSThread currentThread]);
    }];
    [blockOpt start];
}
//2016-08-09 18:51:04.787 NSOperation[48546:758737] 下载1------<NSThread: 0x7fea397053d0>{number = 1, name = main}

一个NSOperation执行start操作,默认在主线程执行

- (void)blockOperation2{
    NSBlockOperation *blockOpt = [NSBlockOperation blockOperationWithBlock:^{
        // 在主线程
        NSLog(@"下载1------%@", [NSThread currentThread]);
    }];
    
    // 添加额外的任务(在子线程执行)
    [blockOpt addExecutionBlock:^{
        NSLog(@"下载2------%@", [NSThread currentThread]);
    }];

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

//2016-08-09 18:57:23.753 NSOperation[48889:764202] 下载1------<NSThread: 0x7fe128d05630>{number = 1, name = main}
//2016-08-09 18:57:23.754 NSOperation[48889:764202] 下载5------<NSThread: 0x7fe128d05630>{number = 1, name = main}
//2016-08-09 18:57:23.754 NSOperation[48889:764202] 下载6------<NSThread: 0x7fe128d05630>{number = 1, name = main}
//2016-08-09 18:57:23.753 NSOperation[48889:764231] 下载3------<NSThread: 0x7fe128f17cd0>{number = 4, name = (null)}
//2016-08-09 18:57:23.753 NSOperation[48889:764238] 下载2------<NSThread: 0x7fe128d379f0>{number = 2, name = (null)}
//2016-08-09 18:57:23.753 NSOperation[48889:764232] 下载4------<NSThread: 0x7fe128ea2c10>{number = 3, name = (null)}

NSBlockOperation封装的block操作数 >1 就会开启子线程异步并发执行

2.NSOperationQueue 与 NSOperation

NSInvocationOperation

- (void)run{
    NSLog(@"run---%@",[NSThread currentThread]);
}
- (void)operationQueueWithInvocationOperation{
    NSInvocationOperation *invOpe = [[NSInvocationOperation alloc] initWithTarget:self
                                                                         selector:@selector(run)
                                                                           object:nil];
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:invOpe];
}
//2016-08-10 11:28:20.993 NSOperationQueue[53590:830749] run---<NSThread: 0x7f94a860d500>{number = 2, name = (null)}
- (void)operationQueueWithBlockOperation{
    NSBlockOperation *blockOpe = [[NSBlockOperation alloc] init];
    [blockOpe addExecutionBlock:^{
        [self run];
    }];
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:blockOpe];
}

//2016-08-10 11:34:22.216 NSOperationQueue[53877:834875] run---<NSThread: 0x7f8dd17011a0>{number = 2, name = (null)}

把1个NSOperation添加到1个NSOperationQueue中,NSOperation封装的操作就会开启一个子线程 在子线程中执行

3.NSOperationQueue 与 自定义NSOperation

ZYXOperation.h

#import <Foundation/Foundation.h>

@interface ZYXOperation : NSOperation
@end

ZYXOperation.m

#import "ZYXOperation.h"

@implementation ZYXOperation

/**
 * 需要执行的任务
 */
- (void)main
{
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download1 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
    
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download2 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
    
    for (NSInteger i = 0; i<5; i++) {
        NSLog(@"download3 -%zd-- %@", i, [NSThread currentThread]);
    }
    if (self.isCancelled) {return;}
}

@end
- (void)operationQueueWithZYXOperation{
    NSOperationQueue *que = [[NSOperationQueue alloc] init];
    [que addOperation:[[ZYXOperation alloc] init]];
}

//2016-08-10 11:41:27.199 NSOperationQueue[54291:841376] download1 -0-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.200 NSOperationQueue[54291:841376] download1 -1-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.200 NSOperationQueue[54291:841376] download1 -2-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.202 NSOperationQueue[54291:841376] download1 -3-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.202 NSOperationQueue[54291:841376] download1 -4-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.203 NSOperationQueue[54291:841376] download2 -0-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.204 NSOperationQueue[54291:841376] download2 -1-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.204 NSOperationQueue[54291:841376] download2 -2-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.205 NSOperationQueue[54291:841376] download2 -3-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.206 NSOperationQueue[54291:841376] download2 -4-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.206 NSOperationQueue[54291:841376] download3 -0-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.207 NSOperationQueue[54291:841376] download3 -1-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.207 NSOperationQueue[54291:841376] download3 -2-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.208 NSOperationQueue[54291:841376] download3 -3-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
//2016-08-10 11:41:27.208 NSOperationQueue[54291:841376] download3 -4-- <NSThread: 0x7fb2aaf1dc50>{number = 2, name = (null)}
- (void)operationQueue{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock:^{
        NSLog(@"download1 --- %@", [NSThread currentThread]);
    }];
}

//2016-08-10 11:53:15.578 NSOperationQueue[54846:848301] download1 --- <NSThread: 0x7ff251d095d0>{number = 2, name = (null)}

4.NSOperationQueue 设置

/** 队列 */
@property (nonatomic, strong) NSOperationQueue *queue;

- (void)handleOperationQueue{
    if (self.queue == nil) {
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:[[ZYXOperation alloc] init]];
        self.queue = queue;
    }
    
    if (self.queue.isSuspended) {
        // 恢复队列,继续执行
        self.queue.suspended = NO;
    } else {
        // 暂停(挂起)队列,暂停执行
        self.queue.suspended = YES;
    }
    
    // 取消掉队列中的操作
    // [self.queue cancelAllOperations];
}

//queue.maxConcurrentOperationCount = 2;
  queue.maxConcurrentOperationCount = 1; // 就变成了串行队列

5.NSOperation之间设置依赖

-(void)setOperationDependency{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download1----%@", [NSThread  currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download2----%@", [NSThread  currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download3----%@", [NSThread  currentThread]);
    }];

    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"download4----%@", [NSThread  currentThread]);
        }
    }];
    
    NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"download5----%@", [NSThread  currentThread]);
    }];
    op5.completionBlock = ^{
        NSLog(@"op5执行完毕---%@", [NSThread currentThread]);
    };
    
    // 设置依赖
    [op3 addDependency:op1];
    [op3 addDependency:op2];
    [op3 addDependency:op4];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];
    [queue addOperation:op5];
}


// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905725] download1----<NSThread: 0x7f9fe1616960>{number = 2, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905978] download5----<NSThread: 0x7f9fe1529cc0>{number = 5, name = (null)}
// 2016-08-10 13:42:50.489 NSOperation-操作依赖[60227:905724] download2----<NSThread: 0x7f9fe1713bd0>{number = 3, name = (null)}
// 2016-08-10 13:42:50.491 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905978] op5执行完毕---<NSThread: 0x7f9fe1529cc0>{number = 5, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.493 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.494 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.494 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.550 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.550 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download4----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}
// 2016-08-10 13:42:50.551 NSOperation-操作依赖[60227:905721] download3----<NSThread: 0x7f9fe16084f0>{number = 4, name = (null)}

6.NSOperationQueue 之间的通信

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/**
 * 线程之间的通信
 */
- (void)threadCommunication{
    [[[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;
        }];
    }];
}
- (void)operationDependencyThreadCommunication{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    __block UIImage *image1 = nil;
    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;
    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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容