iOS开发多线程篇 - NSOperation

2017-03-21  本文已影响11人  码代码的小马

一. NSOperation简介

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

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

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

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

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

2.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}
//
//  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
上一篇下一篇

猜你喜欢

热点阅读