【发布时间】:2012-04-03 09:10:41
【问题描述】:
我的 tableview 的加载时间有问题,我该如何改进?加载大约 90000+ 行的 tableview 大约需要 25-30 秒。我的 sqlite 数据库中有大约 130000 行,我使用核心数据访问这些数据。
任何帮助或建议将不胜感激。
感谢您抽出宝贵时间,如果您需要更多信息,请告诉我。
我一直在努力优化两件事。
+核心数据 - 也许我的数据获取可以改进,但我不知道我还能做什么,代码如下。
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataModel" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"subsection" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
//[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"section == [c] %@ AND area == 'Tables'", tablesSection]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"area == 'Tables'"]];
[fetchRequest setFetchBatchSize:100];
//[fetchRequest setFetchLimit:100];
[fetchRequest setFetchOffset:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:@"uppercaseFirstLetterOfNameTables" cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
fetchedResultsController.delegate = self;
return fetchedResultsController;
+tableview,我认为我加载数据的方式可能是这里的主要问题。我动态计算每个单元格的高度,我认为这会减慢速度,如果我删除方法 heightForRowAtIndexPath 的代码,我可以将加载时间缩短到 8 秒。
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Model *info;
if(self.searchDisplayController.isActive)
{
info = [searchFetchedResultsController objectAtIndexPath:indexPath];
}
else
{
info = [fetchedResultsController objectAtIndexPath:indexPath];
}
NSString *subsectionHeight = info.subsection;
NSString *textHeight = info.text;
CGSize titleSize = [subsectionHeight sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:20] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [textHeight sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:16] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
//return 88;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;
}
//if-statement need to prevent search index issue
if(self.searchDisplayController.isActive && [self.searchDisplayController.searchBar.text isEqualToString:@""])
{
return cell;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Model *info;
if (self.searchDisplayController.isActive)
{
info = [searchFetchedResultsController objectAtIndexPath:indexPath];
}
else
{
info = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.textLabel.text = info.subsection;
cell.detailTextLabel.text = info.text;
}
更新:我正在使用注释掉 heightForRowAtIndexPath 的解决方案,但我需要搜索优化方面的帮助,有人可以帮助我了解如何在 WWDC 2010 session 137 Optimizing Core Data Performance 中实施该解决方案,我不明白创建新实体部分,例如我需要用数据填充那个新实体吗?
【问题讨论】:
-
90000+ 行对于单个表视图来说是荒谬的。没有人能够滚动到最后。将您的数据分成多个部分并具有导航结构。
-
我同意单个 tableview 的 90000+ 行是疯狂的,但不幸的是这是给我的,我很想分解数据,但我不知道如何做到这一点,因为它的 SAP 表数据。
标签: ios uitableview core-data