iOS开发多线程篇 - NSOperation

一. NSOperation简介

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

    1. 先将需要执行的操作封装到一个NSOperation对象中
    2. 然后将NSOperation对象添加到NSOperationQueue
    3. 系统会自动将NSOPerationQueue中的NSOperation取出来
    4. 将取出的NSOperation封装的操作放到一条新线程中执行

2.NSoperation的子类
- NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类
- 使用NSOperation子类的方式有三种

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

二.具体说明

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

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

    [operation start];

  ```
说明:一旦执行操作,就会调用`Target`的`testTarget`方法

代码示例:

//
// 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—{number = 1, name = main}—


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

2.**`NSBlockOperation`**子类

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

}];

operation addExecutionBlock:^{

    //......
};

2. 代码示例:

    -  代码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——{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——{number = 4, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115571] NSBlockOperation——{number = 3, name = (null)}
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115529] NSBlockOperation2——{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—- {number = 4, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159391] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159394] —–NSBlockOperation–3–1—- {number = 5, name = (null)}
2017-03-21 13:36:39.594 TestNSOperationQueue[5185:159414] —–NSBlockOperation–3–2—- {number = 6, name = (null)}


> 注意:系统自动将`NSOperationQueue中``NSOperation`对象取出,将其封装的操作放到一条新的线程中执行,上面的代码示例一共有四个任务,`operation1``operation2`分别有一个任务,`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—- {number = 5, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.090 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.091 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.094 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.096 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.099 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.100 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.101 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.103 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.105 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.106 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.108 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {number = 6, name = (null)}
2017-03-21 13:51:44.112 TestNSOperationQueue[5519:170579] —–NSBlockOperation—-3–1—- {number = 5, name = (null)}
2017-03-21 13:51:44.113 TestNSOperationQueue[5519:170580] —–NSInvocationOperation–1—- {number = 3, name = (null)}
2017-03-21 13:51:44.117 TestNSOperationQueue[5519:170582] —–NSInvocationOperation–2—- {number = 4, name = (null)}
2017-03-21 13:51:44.119 TestNSOperationQueue[5519:170644] —–NSBlockOperation—-3–2—- {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

基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip基于MATLAB实现旅行推销员问题(TSP)的代码+项目说明(课程大作业)+测试数据.zip 【备注】 1、该资源内项目代码百分百可运行,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值