app store中的很多应用程序非常的笨重,他们有好的界面,但操作性很差,比如说当程序从网上或本地载入数据的时候,界面被冻结了,用户只能等程序完全载入数据之后才能进行操作。
当打开一个应用程序时,iphone会产生一个包含main方法的线程,所用程序中的界面都是运行在这个线程之中的(table views, tab bars, alerts…),有时候我们会用数据填充这些view,现在问题是如何有效的载入数据,并且用户还能自如的操作程序。
下面要说方法的并不是要在用户载入数据的时候在界面上提示“loading”的信息,虽然这种方式在有些时候是可以被接受的,但当数据在main线程之外被载入是并不是最有效的方式。
先看一下要演示的程序:

这个程序将从网络上下载10,000条数据,并填入到UITableView中,现面的代码将首先演示一种错误的方式:
错误 (源码 )


@implementationRootViewController
@synthesizearray;-(void)viewDidLoad {[super viewDidLoad];/* Adding the button */self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"Load"style:UIBarButtonItemStyleDone
target:selfaction:@selector(loadData)];/* Initialize our array */NSMutableArray*_array =[[NSMutableArrayalloc]initWithCapacity:10000];
self.array=_array;
[_array release];
}// Fires when the user presses the load button-(void)loadData {/* Grab web data */NSURL*dataURL =[NSURLURLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"];NSArray*tmp_array =[NSArrayarrayWithContentsOfURL:dataURL];/* Populate our array with the web data */for(NSString*str intmp_array){[self.arrayaddObject:str];
}/* reload the table */[self.tableViewreloadData];
}#pragma mark Table view methods-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return1;
}-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {return[self.arraycount];
}-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {staticNSString*CellIdentifier =@"Cell";UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]autorelease];
}/* Display the text of the array */[cell.textLabel setText:[self.arrayobjectAtIndex:indexPath.row]];returncell;
}-(void)dealloc{[super dealloc];
[arrayrelease];
}@end

当点击“load”按钮时程序会被冻结,直到将数据完全下载并填入Tableview,在这期间用户不能做任何的事情。
在给出解决方式之前先来看一下NSOperationQueue和NSOperation:

The NSOperation and NSOperationQueue classes  alleviate much of the pain of multi-threading, allowing you to simply  define your tasks, set any dependencies that exist, and fire them off.  Each task, or operation , is represented by an instance  of an NSOperation class; the NSOperationQueueclass  takes care of starting the operations, ensuring that they are run in  the appropriate order, and accounting for any priorities that have been  set.

下面要做的是建立NSOperationQueue和NSOperations。NSOperationQueue会建立一个线程,每个加入到线程operation会有序的执行。
下面是使用NSOperationQueue的过程:

  1. 建立一个NSOperationQueue的对象
  2. 建立一个NSOperation的对象
  3. 将operation加入到NSOperationQueue中
  4. release掉operation

使用NSOperation有几种,现在介绍最简单的一种NSInvocationOperation,NSInvocationOperation是NSOperation的子类,允许运行在operation中的targer和selector。  下面是执行NSInvocationOperation的一个例子:


NSOperationQueue*queue =[NSOperationQueuenew];NSInvocationOperation*operation=[[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(methodToCall)object:objectToPassToMethod];[queue addOperation:operation];
[operationrelease];

下面是我们用正确的方式实现的程序:
正确的方式(下载源码 )


@implementationRootViewController
@synthesizearray;-(void)viewDidLoad {[super viewDidLoad];self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"Load"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(loadData)];NSMutableArray*_array =[[NSMutableArrayalloc]initWithCapacity:10000];self.array=_array;[_array release];
}-(void)loadData {/* Operation Queue init (autorelease) */NSOperationQueue*queue =[NSOperationQueuenew];/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */NSInvocationOperation*operation=[[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(loadDataWithOperation)object:nil];/* Add the operation to the queue */[queue addOperation:operation];[operationrelease];
}-(void)loadDataWithOperation {NSURL*dataURL =[NSURLURLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"];NSArray*tmp_array =[NSArrayarrayWithContentsOfURL:dataURL];for(NSString*str intmp_array){[self.arrayaddObject:str];}[self.tableViewreloadData];
}#pragma mark Table view methods-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return1;
}-(NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section {return[self.arraycount];
}-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {staticNSString*CellIdentifier =@"Cell";UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];if(cell==nil){cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];}[cell.textLabel setText:[self.arrayobjectAtIndex:indexPath.row]];returncell;
}-(void)dealloc{[super dealloc];[arrayrelease];
}

再次运行程序,当点击“load”按钮时界面是否还被“冻结”呢,程序并没有增加很多的代码,但确大大的提高了用户体验。

转载于:https://www.cnblogs.com/zhwl/archive/2013/01/05/2845281.html

使用NSOperation为你的app加速相关推荐

  1. 这是一个为你的“所有APP”加速的APP

    近日SASE(Secure Access Service Edge)安全访问服务边缘,以其"身份驱动.云原生.支持所有边缘(WAN.云.移动.边缘计算).全球分布"的特点,一度在业 ...

  2. 茄子科技(海外SHAREit Group)赋能出海APP加速布局新兴市场

  3. 使用七牛云对网站进行加速基本配置

    看了很多给网站加速的教程,大同小异,觉得还是使用免费的七牛云进行CDN加速,去年使用这个平台给APP加速,效果还不错. 1.打开七牛云存储注册链接:七牛云 注册帐号并且完成系统要求的实名认证要求.一般 ...

  4. APU(美国AMD公司研发的加速处理器)

    APU(Accelerated Processing Unit)中文名字叫加速处理器,是AMD"融聚未来"理念的产品,它第一次将中央处理器和独显核心做在一个晶片上,它同时具有高性能 ...

  5. cordova 更改app版本_【ios马甲包cps联运】App上架难 马甲包不知道该怎么做?

    专业app代上架!解决全网IOS上包难诸多问题 ,提供多类别马甲包功能包定制服务!(直播.财务.社交.生活.游戏.电商)另外提供app加速审核及好评优化服务.长期出售白包功能包! 总的来说,App S ...

  6. 2022 App内测分发五大趋势:进一步提升开发者生产力

    如今,App已经成为连接人与人.人与世界的基础,一个小小功能的改动都可能推动信息传递与生产效率的大幅增加.然而,随着信息化的发展愈发成熟,互联网的人口红利正在加速消逝,各App从疯狂拉新转向对存量用户 ...

  7. 信用卡市场发展洞察:浦大喜奔APP探索大零售融合经营体系

    易观分析:信用卡业务发展历经高速发展后增速回落,跑马圈地带来规模爆发增长后面临向精细化经营竞争转变.伴随信用卡新规落实,进一步推动信用卡业务向专业化.差异化.精细化经营发展.同时,国家"大统 ...

  8. ASO优化技巧:苹果加急审核注意事项,app store的aso怎么做

    今天利用空闲之余,跟大家聊聊苹果AppStore一些不为人知的事情--加急审核,或许对iOS端App开发的小伙伴有些小帮助. 一.App Store应用审核规则 1. 审核时间规律 应用提交到App ...

  9. 2021年证券类APP更新迭代监测专题分析(中)发布

    易观分析:2021年,证券类APP加速迭代,易观分析分别从行情.资讯.交易.理财.界面.智能工具六大维度监测证券类APP迭代方向,现累计监测多款证券类APP,客观呈现证券类APP更新监测现状. ▼ 共 ...

最新文章

  1. react-navigation
  2. sql查询第二大的记录(转)
  3. 二十五:设计模式的总结
  4. 今年,你会为5G消费吗?就一分钟,求投票
  5. [转] 用Firebug调试JavaScript
  6. Spring MVC Servlet XML文件配置
  7. 使用Servlet实现用户注册
  8. 基于ansj_seg和nlp-lang的简单nlp工具类
  9. MySQL定义数据库对象之指定definer
  10. 推荐一个很好的富文本web编辑器UEditor
  11. flink安装以及运行自带wordcount示例(单机版,无hadoop环境)
  12. python函数名的应用、闭包和迭代器
  13. SqlServer获取当前日期
  14. HTML5 WebGame开源工具之impactjs
  15. Java8新特性Stream之list转map
  16. args在python中什么意思_Python中*args、**args到底是什么、有啥区别、怎么用
  17. matlab三个简单物理建模实例(笔记)
  18. 在GitHub中搜索的技巧
  19. android jdk和ndk下载地址,cocos2d-x Android(SDK,NDK,JDK,ANT)下载地址
  20. 测试点设计及编写思路

热门文章

  1. android+微信一键关注,一键关注微信公众平台JS代码有哪些?
  2. mysql etl工具有哪些_常见ETL工具一览,你知多少?
  3. linux内核镜像sd卡,【原创】Linux QT镜像的制作--制作SD卡启动盘
  4. java 解析http返回的xml_Java解析调用webservice服务的返回XML串详解
  5. html中选择省份城市,省份、城市、区县三级联动Html代码
  6. Exception in thread main com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport e
  7. opencv 滤镜效果php,OpenCV实现马赛克和毛玻璃滤镜效果
  8. (十四)nodejs循序渐进-高性能游戏服务器框架pomelo之开发Treasures游戏
  9. Redis:07---Redis数据结构
  10. 《Python Cookbook 3rd》笔记(3.8):分数运算