app中的一个TableView,使用原生的UILocalizedIndexedCollation进行索引排序(A-Z),实测速度很慢,400多条数据需要3秒多才能显示出来
定位后发现,瓶颈不在数据库访问和UI渲染上,就是索引排序太慢。优化前有性能问题的代码如下:
// slow method
-(void) assembleMembers:(NSArray*)origin
{
[members removeAllObjects];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
// slow point 1: takes 1.5 seconds when 400 records
for (Member *member in origin) {
NSInteger sect = [collation sectionForObject:member collationStringSelector:@selector(name)];
member.sectionNumber = sect;
}
NSInteger highSection = [[collation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for (int i = 0; i < highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
[sectionArrays addObject:sectionArray];
}
for (Member *member in origin) {
[(NSMutableArray*)[sectionArrays objectAtIndex:member.sectionNumber] addObject:member];
}
// slow point 2: takes 1.3 seconds when 400 records
for (NSMutableArray *sectionArray in sectionArrays) {
NSArray *sortedSection = [collation sortedArrayFromArray:sectionArray collationStringSelector:@selector(name)];
[members addObject:sortedSection];
}
}
有2段很慢,第一段是给Member分配sectionNumber,第二段是对26个子数组进行深度排序
发现了瓶颈,就针对瓶颈进行优化
分配sectionNumber的逻辑,放到每次插入数据库时。在表中增加section_number字段,保存这个值。这样每次从数据库取到的数据,就不需要在运行时排序了
深度排序耗时长的问题,改成首字母排序,速度也快了很多
最后的代码如下:
-(void) assembleMembers:(NSArray*)origin
{
[members removeAllObjects];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger highSection = [[collation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for (int i = 0; i < highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
[sectionArrays addObject:sectionArray];
}
for (Member *member in origin) {
[(NSMutableArray*)[sectionArrays objectAtIndex:member.sectionNumber] addObject:member];
}
for (NSMutableArray *sectionArray in sectionArrays) {
[sectionArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Member *m1 = (Member*) obj1;
Member *m2 = (Member*) obj2;
return [m2.name localizedCompare:m1.name];
}];
[members addObject:sectionArray];
}
}
这样改完之后,400条数据的处理时间缩短到0.3秒,基本可以接受了
分享到:
相关推荐
优化tableview加载性能,流畅度,高仿微博等主流新闻客户端的加载模式。
iOS tableview 索引
使用RunLoop优化tableView加载大量图片卡顿问题,可有效解决主线程阻塞问题
UITableView内部的UITableViewIndex,实现滑动事件的监听回调
tableview点击排序
这是一个关于tableView索引栏的demo,对代码做了优化,有兴趣的可以瞅瞅!
RCP 中TableView对列进行排序,实例代码,很方便,欢迎下载,好东西大家一起分享。谢谢。
通过runtime修改系统的tableview索引的点击效果,一句代码就可以集成,支持自定义动画效果。github上有代码的使用demo。 git:https://github.com/coolboy-ccp/CCPIndexView,有用到的兄弟star下。
这是一个很简单的tableView右侧索引的小demo。没有任何封装 只是简单的实现
iphone 开发-一个带索引和搜索功能的tableview例子,清晰的展示了功能实现的方法,简单易懂。
searchBar 和 tableView快速索引的结合使用 如发现问题请发邮件至ranheran@sohu.com,我会尽快修正
实现了Qt QTableWidget基本的添加删除修改等功能和添加了点击表头排序功能
一个iOS的第三方文件,里面的方法可以实现汉字按照首字母排序,并且可以自动生成索引字母数组和各section的二级数组.
uitableview 侧边的 数据索引 滑动提示在哪个分组 点击会跳到相对应的组中
TableView滑动优化加载在图片(SDWebImage+Runloop)
GroupTableSeparatorFix:分组TableView分割线优化
UITableViewIndexBar、快速索引栏、侧边栏、字母索引栏、通讯录、可以自定义各种效果以及动画
之前一直做Cocos2dx开发的TableView用得比较多,自己也扩展了一些功能,新公司用的Creator,所以翻译了个ts版本的,核心代码和函数名与cocos2dx的一致。 例子:...
这个小项目中,有sqlite的应用,tableView的查找搜索与优化,就像微信中的联系人一样,有自动布局的应用……
swift ,使用autolayout + storyboard 最外层tableview列表,cell里面嵌套了tableview,自适应内容,cell里的tableview不可滑动,内容全部显示,且文字分行显示,不用设置cell的高度直接可以自适应内容