在ios中,使用多线程有三种方式,分别是:NSThread、NSOperation和NSOperationQueue、GCD,在本节,主要讲解一下NSOperation的使用。

  NSOperation和NSOperationQueue这种方式实际上是将NSOperation的对象放到一个NSOperationQueue队列中,然后依次启动操作,类似于线程池的使用。

  在使用的过程中,NSOperation的操作使用的是它的子类,分别是NSInvocationOperation和NSBlockOperation,两者没有本质的区别,只不过后者以Block的方式来实现,使用相对简单。NSOperationQueue主要负责管理和执行所有的NSOperation对象,并控制线程之间的执行顺序与依赖关系。

  下面,通过NSOperation开始多线程从网络获取图片并刷新。

NSInvocationOperation

代码

//  ViewController.m
// AAAAAA
//
// Created by jerei on 15-11-8.
// Copyright (c) 2015年 jerehedu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
} #pragma mark - 点击按钮开启线程下载图片
- (IBAction)click_InvocationOpreation_load:(UIButton *)sender { NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"]; //创建一个operation
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImageWithUrl:) object:url]; //添加到操作队列中
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
} #pragma mark - 根据url获取图片
-(void)loadImageWithUrl:(NSURL *)url{ NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data]; //回到主线程更新界面
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateImageView:) object:image]; [[NSOperationQueue mainQueue] addOperation:operation];
} #pragma mark - 更新界面
-(void)updateImageView:(UIImage *)img{
_imageView.image = img;
} @end

NSBlockOperation

代码

//  ViewController.m
// AAAAAA
//
// Created by jerei on 15-11-8.
// Copyright (c) 2015年 jerehedu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
} #pragma mark - 点击按钮开启线程下载图片
- (IBAction)click_BlockOpreation_load:(UIButton *)sender { //创建操作队列
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//设置最大并发线程数
operationQueue.maxConcurrentOperationCount = ; //<方法一> 创建operation
// NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
// //根据url请求数据
// NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"];
// [self loadImageWithUrl:url];
// }];
//
// //添加到队列中
// [operationQueue addOperation:operation]; //<方法二> 创建operation
[operationQueue addOperationWithBlock:^{
//根据url请求数据
NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"];
[self loadImageWithUrl:url];
}];
} #pragma mark - 根据url获取图片
-(void)loadImageWithUrl:(NSURL *)url{ NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data]; //回到主线程更新界面
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateImageView:image];
}];
} #pragma mark - 更新界面
-(void)updateImageView:(UIImage *)img{
_imageView.image = img;
} @end
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

iOS中的多线程 NSOperation的更多相关文章

  1. OS X 和iOS 中的多线程技术(下)

    OS X 和iOS 中的多线程技术(下) 上篇文章中介绍了 pthread 和 NSThread 两种多线程的方式,本文将继续介绍 GCD 和 NSOperation 这两种方式.. 1.GCD 1. ...

  2. OS X 和iOS 中的多线程技术(上)

    OS X 和iOS 中的多线程技术(上) 本文梳理了OS X 和iOS 系统中提供的多线程技术.并且对这些技术的使用给出了一些实用的建议. 多线程的目的:通过并发执行提高 CPU 的使用效率,进而提供 ...

  3. IOS中的多线程和NSRunLoop概述(转载)

    线程概述 有些程序是一条直线,从起点到终点,如Hello World,运行打印完,它的生命周期便结束了:有些程序是一个圆,不断循环,直到将它切断,如操作系统,一直运行直到你关机.  一个运行着的程序就 ...

  4. iOS中实现多线程的技术方案

    pthread 实现多线程操作 代码实现: void * run(void *param) {    for (NSInteger i = 0; i < 1000; i++) {         ...

  5. iOS中的多线程NSThread/GCD/NSOperation & NSOperationQueue

    iOS多线程有四套多线程方案: Pthreads NSThread GCD NSOperation & NSOperationQueue 接下来我来一个一个介绍他们 Pthreads 在类Un ...

  6. iOS中的多线程及GCD

    多线程中的一些概念 //任务:代码段  方法  线程就是执行这些任务 //NSThread类 创建线程 执行线程 [NSThread isMainThread]//判断是否是主线程 #import & ...

  7. iOS中的多线程基础

    NSThread NSThread是一个苹果封装过的,面向对象的线程对象.但是它的生命周期需要我们自己来手动管理,所以使用不是很常见,比如[NSThread currentThread],它可以获取当 ...

  8. IOS编程之多线程

    IOS编程之多线程 目录 概述——对多线程的理解 IOS中实现多线程的三种方式 NSThread 线程创建 线程的同步与锁 线程间的交互 线程的操作方法 NSOperation and NSOpera ...

  9. iOS中 图文混排/自定义图文混排 作者:韩俊强

    指示根视图:(准备几张图片,把label加载在window上) CustomLable *label = [[CustomLable alloc]initWithFrame:CGRectMake(0, ...

随机推荐

  1. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  2. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  3. BZOJ 4610: [Wf2016]Ceiling Functi 水题

    4610: [Wf2016]Ceiling Functi 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4610 Description ...

  4. 自己封装jquery的一些方法 链式调用模式

    function getIndex(ele){ var parent=ele.parentNode; var brothers=parent.children; for(var i=0,len=bro ...

  5. STM32F4, USB HS with ULPI and Suspend/Wakeup

    Hi guys,I am in need of your help, unfortunately STs documentation is lacking some information here. ...

  6. Revit API布置卫浴装置

    //放置卫浴装置 [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public clas ...

  7. 体验h5离线缓存

    摘要 Application Cache是浏览器自己的一种机制,随着移动互联网时代的到来,如果我们已经将需要的文件缓存下下来,一旦网络无法访问,也能继续访问.不仅能提高用户体验,而且在有网络时,也能直 ...

  8. 利用npm 安装删除模块

    转自 涵一原文 利用npm 安装删除模块 1. npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录:[npm install -g xxx]利用npm安 ...

  9. WordPress基础:get_page_link获取页面地址

    函数:get_page_link(页面id编号) 作用:获取指定页面的链接地址 用法: $link = get_page_link(2); 输出为:xxx/?page_id=2 如在循环里则不用填写i ...

  10. T4:使用 T4 消除程序和配置文件中重复的字符串信息

    背景 我们经常在配置文件中配置各种:id.name,然后在程序中使用这些配置获取信息,这导致了字符串重复出现在系统的多个地方,非常不利于维护,本文介绍采用 T4 来消除这种重复. T4 消除重复 配置 ...