使用带有sectionNameKeyPath的NSFetchedResultsController的性能问题

时间:2014-06-04 08:59:21

标签: ios uitableview core-data nsfetchedresultscontroller

我在CoreData中有大量数据(大约10.000个对象)。

我想在各个部分展示它们,因此我使用:

+ (NSFetchedResultsController*) eventsFetchResultControllerWithSearchString:(NSString*) searchString andFilter:(EventFilters) filter
{
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"ts" ascending:NO];
NSArray* sortDescriptors = @[sort];

NSPredicate* filterPredicate = [NSFetchedResultsController eventsPredicatesWithSearchString:searchString andFilter:filter];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *callEntity = [NSEntityDescription entityForName:@"EventDB" inManagedObjectContext:[[AppManager sharedAppManager] managedObjectContext]];
[fetchRequest setEntity:callEntity];
[fetchRequest setPredicate:filterPredicate];
[fetchRequest setFetchBatchSize:20];
[fetchRequest setSortDescriptors:sortDescriptors];

NSString* sectionKeyPath = @"timestamp.relativeDate";
if(searchString)
{
    sectionKeyPath = nil;
}

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                             managedObjectContext:[[AppManager sharedAppManager] managedObjectContext]
                                                                                               sectionNameKeyPath:sectionKeyPath
                                                                                                        cacheName:nil];

NSError *error = nil;
if (![aFetchedResultsController performFetch:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return aFetchedResultsController;
}

在我的UITableView上我有:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat emptyCellHeight = [super tableView:tableView noDataCellHieghtAtIndexPath:indexPath forCellType:kUITableViewCellTypeNoDataCell];
if(emptyCellHeight != 0.0f)
{
    return emptyCellHeight;
}

if(self.measurementCell == nil)
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EventCell" owner:self options:nil];
    self.measurementCell = [topLevelObjects objectAtIndex:0];
}

EventDB *event = [[self fetchedResultsControllerForTableView:tableView] objectAtIndexPath:indexPath];
return [self.measurementCell calculateHeightForEvent:event];
}

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(self.tableView == tableView && [[self fetchedResultsControllerForTableView:tableView].sections count] == 0)
{
    return 1;
}

return [[[self fetchedResultsControllerForTableView:tableView] sections] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray* sections = [[self fetchedResultsControllerForTableView:tableView] sections];
if(section < [sections count])
{
    id <NSFetchedResultsSectionInfo> theSection = [sections objectAtIndex:section];
    return [theSection name];
}

return nil;
}

我找到了一个使用estimatedHeight改善高度计算的解决方案,但是当我打开sectionNameKeyPath时,它变得非常慢。

我怀疑是因为它加载所有元素以在开始时读取所有部分。

有没有办法优化那部分?有什么像estimatedHeight但是对于部分?

0 个答案:

没有答案