iOS开发多线程篇 - NSOperation

一. NSOperation简介

  1. 简单说明
    NSOperation的作用:配合使用NSOperationNSOperationQueue也能实现多线程编程
    NSOperationNSOperationQueue实现多线程的具体步骤:

  2. 先将需要执行的操作封装到一个NSOperation对象中

  3. 然后将NSOperation对象添加到NSOperationQueue

  4. 系统会自动将NSOPerationQueue中的NSOperation取出来

  5. 将取出的NSOperation封装的操作放到一条新线程中执行

2.NSoperation的子类

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

  • 使用NSOperation子类的方式有三种

  • NSInvocationOperation

  • NSBlockOperation

  • 自定义子类继承NSOperation,实现内部相应的方法

二.具体说明

1.NSInvocationOperation子类
创建对象和执行操作:

    NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testTarget) object:nil];
    
    [operation start];

说明:一旦执行操作,就会调用TargettestTarget方法

代码示例:

//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testTarget) object:nil];
    
    [operation start];
}

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

打印结果:

2017-03-21 11:16:05.385 TestNSOperationQueue[3648:99757] -------test---<NSThread: 0x6080000775c0>{number = 1, name = main}---

注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程,即默认情况下,如果操作没有放到队列中queue中,都是同步执行,只有将NSOperation放到一个NSOperationQueue中,才会以异步执行

2.NSBlockOperation子类

  1. 创建对象和添加操作:
    NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
        //......
        
    }];

    operation addExecutionBlock:^{
      
        //......
    };

  1. 代码示例:

    • 代码1:
//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
        
    }];

    [operation start];
    
    
}

@end

打印结果:

2017-03-21 11:37:21.540 TestNSOperationQueue[4033:113489] NSBlockOperation------<NSThread: 0x60800006a3c0>{number = 1, name = main}
  • 代码2:
//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
        
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"NSBlockOperation1------%@",[NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"NSBlockOperation2------%@",[NSThread currentThread]);
    }];

    [operation start];
    
    
}

@end

打印结果:

2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115570] NSBlockOperation1------<NSThread: 0x608000261240>{number = 4, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115571] NSBlockOperation------<NSThread: 0x600000267dc0>{number = 3, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115529] NSBlockOperation2------<NSThread: 0x60800007dc00>{number = 1, name = main}

注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作

3.NSOperationQueue
NSOperationQueue的作用:NSOperation可以调用start方法来执行,但默认是同步执行的

如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中,自动执行操作,自动开启线程

    NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
    NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];
    NSInvocationOperation * operation3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation3) object:nil];
    
    //创建NSOperationQueue
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    
    //把操作添加到队列中
    //第一种方式
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    
    //第二种方式
    [queue addOperationWithBlock:^{
        NSLog(@"-------testOperationBlock-----");
    }];

代码示例:

//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建NSInvocationOperation对象,封装操作
    NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
    NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];
    
    //创建对象,封装操作
    NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"-----NSBlockOperation--3--1---- %@",[NSThread currentThread]);
    }];
    
    [operation3 addExecutionBlock:^{
        NSLog(@"-----NSBlockOperation--3--2---- %@",[NSThread currentThread]);
    }];
    
    //创建NSOperationQueue
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    
    //把操作添加到队列中
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    
}

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

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


@end

打印效果:

2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159392] -----NSInvocationOperation--2---- <NSThread: 0x60800007b480>{number = 4, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159391] -----NSInvocationOperation--1---- <NSThread: 0x600000269f40>{number = 3, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159394] -----NSBlockOperation--3--1---- <NSThread: 0x60000026a0c0>{number = 5, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159414] -----NSBlockOperation--3--2---- <NSThread: 0x608000266240>{number = 6, name = (null)}

注意:系统自动将NSOperationQueue中NSOperation对象取出,将其封装的操作放到一条新的线程中执行,上面的代码示例一共有四个任务,operation1operation2分别有一个任务,operation3有2个任务。一共四个任务,开启了四条线程,通过任务执行的时间全部是2017-03-21 13:36:39.594可以看出,这些任务是并行执行的

提示:队列的取出是有顺序的,与打印结果并不矛盾。这就好比,选手A,B,C虽然起跑的顺序是先A,后B,然后C,但是到达终点的顺序却不一定是A,B在前,C在后。

下面使用for循环打印,可以更明显的看出任务是并行执行的

代码示例:

//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建NSInvocationOperation对象,封装操作
    NSInvocationOperation * operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation1) object:nil];
    NSInvocationOperation * operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testOperation2) object:nil];
    
    //创建对象,封装操作
    NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
        
        for (int i = 0; i < 5; i++) {
        NSLog(@"-----NSBlockOperation----3--1---- %@",[NSThread currentThread]);
        }
    }];
    
    [operation3 addExecutionBlock:^{
        for (int i = 0; i < 5; i++) {
        NSLog(@"-----NSBlockOperation----3--2---- %@",[NSThread currentThread]);
        }
        
    }];
    
    //创建NSOperationQueue
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    
    //把操作添加到队列中
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    
}

-(void)testOperation1
{
    for (int i = 0; i < 5; i++) {
    NSLog(@"-----NSInvocationOperation--1---- %@",[NSThread currentThread]);
    }
    
}

-(void)testOperation2
{
    for (int i = 0; i < 5; i++) {
     NSLog(@"-----NSInvocationOperation--2---- %@",[NSThread currentThread]);
    }
}


@end

打印结果:

2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170579] -----NSBlockOperation----3--1---- <NSThread: 0x608000074f80>{number = 5, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170582] -----NSInvocationOperation--2---- <NSThread: 0x608000075140>{number = 4, name = (null)}
2017-03-21 13:51:44.090 TestNSOperationQueue[5519:170580] -----NSInvocationOperation--1---- <NSThread: 0x60800006f200>{number = 3, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170644] -----NSBlockOperation----3--2---- <NSThread: 0x6080000752c0>{number = 6, name = (null)}
2017-03-21 13:51:44.094 TestNSOperationQueue[5519:170579] -----NSBlockOperation----3--1---- <NSThread: 0x608000074f80>{number = 5, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170580] -----NSInvocationOperation--1---- <NSThread: 0x60800006f200>{number = 3, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170582] -----NSInvocationOperation--2---- <NSThread: 0x608000075140>{number = 4, name = (null)}
2017-03-21 13:51:44.099 TestNSOperationQueue[5519:170644] -----NSBlockOperation----3--2---- <NSThread: 0x6080000752c0>{number = 6, name = (null)}
2017-03-21 13:51:44.100 TestNSOperationQueue[5519:170579] -----NSBlockOperation----3--1---- <NSThread: 0x608000074f80>{number = 5, name = (null)}
2017-03-21 13:51:44.101 TestNSOperationQueue[5519:170580] -----NSInvocationOperation--1---- <NSThread: 0x60800006f200>{number = 3, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170582] -----NSInvocationOperation--2---- <NSThread: 0x608000075140>{number = 4, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170644] -----NSBlockOperation----3--2---- <NSThread: 0x6080000752c0>{number = 6, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170579] -----NSBlockOperation----3--1---- <NSThread: 0x608000074f80>{number = 5, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170580] -----NSInvocationOperation--1---- <NSThread: 0x60800006f200>{number = 3, name = (null)}
2017-03-21 13:51:44.106 TestNSOperationQueue[5519:170582] -----NSInvocationOperation--2---- <NSThread: 0x608000075140>{number = 4, name = (null)}
2017-03-21 13:51:44.108 TestNSOperationQueue[5519:170644] -----NSBlockOperation----3--2---- <NSThread: 0x6080000752c0>{number = 6, name = (null)}
2017-03-21 13:51:44.112 TestNSOperationQueue[5519:170579] -----NSBlockOperation----3--1---- <NSThread: 0x608000074f80>{number = 5, name = (null)}
2017-03-21 13:51:44.113 TestNSOperationQueue[5519:170580] -----NSInvocationOperation--1---- <NSThread: 0x60800006f200>{number = 3, name = (null)}
2017-03-21 13:51:44.117 TestNSOperationQueue[5519:170582] -----NSInvocationOperation--2---- <NSThread: 0x608000075140>{number = 4, name = (null)}
2017-03-21 13:51:44.119 TestNSOperationQueue[5519:170644] -----NSBlockOperation----3--2---- <NSThread: 0x6080000752c0>{number = 6, name = (null)}

实战: 实现一个下载图片的例子

//
//  ViewController.m
//  TestNSOperationQueue
//
//  Created by taobaichi on 2017/3/21.
//  Copyright © 2017年 MaChao. All rights reserved.
//

#import "ViewController.h"

#define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"

@interface ViewController ()

@property (nonatomic, strong) UIImageView * imagView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imagView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 50, self.view.frame.size.height/2 - 50, 100, 100)];
    self.imagView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.imagView];
    
    //创建一个后台线程,后台线程执行downloadImage方法
    NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];
    
    //创建NSOperationQueue
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    
    //把创建的后台线程放到NSOperationQueue中
    [queue addOperation:operation];
    
}

-(void)downloadImage:(NSString *)url
{
    NSLog(@"url: %@",url);
    NSURL * nsUrl = [NSURL URLWithString:url];
    NSData * data = [[NSData alloc]initWithContentsOfURL:nsUrl];
    UIImage * image = [[UIImage alloc]initWithData:data];
    
    //下载完成后执行主线程updateUI方法
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}

-(void)updateUI:(UIImage *)image
{
    self.imagView.image = image;
}

@end

代码注释:

  1. viewDidLoad方法里可以看到我们用NSInvocationOperation建了一个后台线程,并且放到NSOperationQueue中。后台线程执行downloadImage方法。
  2. downloadImage 方法处理下载图片的逻辑。下载完成后用performSelectorOnMainThread执行主线程updateUI方法。
  3. updateUI 并把下载的图片显示到图片控件中。

运行后可以看到下载图片显示在界面上

Simulator Screen Shot 2017年3月21日 14.29.48.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,271评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,725评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,252评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,634评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,549评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,985评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,471评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,128评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,257评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,233评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,235评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,940评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,528评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,623评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,858评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,245评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,790评论 2 339

推荐阅读更多精彩内容