我有一些方法需要一些时间来处理获取数据、执行计算然后返回结果,例如这个实例方法返回一个基于三个参数的数组:
-(NSArray*)periodsForCompanies:(NSArray*)companies figureType:(NSString*)figureType
actualValuesOnly:(BOOL)actualValuesOnly
{.. };
由于此方法可以使用同一类的相同参数多次调用并且需要一些时间才能完成,因此我想优化我的代码以避免每次都完全执行此方法的代码。
我如何将先前的参数(和/或结果)存储在方法中,以便能够将当前参数与先前的参数进行比较并决定是否需要再次执行代码?在这种情况下,什么是“最佳实践”?
我的理解是,当调用该方法时,通常他方法中的所有变量都会重置为零和零。
编辑
根据要求,我添加了一个代码示例:
- (NSArray*)periodsForCompanies:(NSArray*)companies figureType:(NSString*)figureType actualValuesOnly:(BOOL)actualValuesOnly
{
// fetch all periods
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"IBEstPeriod"];
NSPredicate *predicate = [[NSPredicate alloc]init];
if (actualValuesOnly == YES)
predicate = [NSPredicate predicateWithFormat:@"(company IN %@) AND (ANY estType.actValue != nil) AND (ANY estType.type == %@)", self.companies, figureType];
else
predicate = [NSPredicate predicateWithFormat:@"(company IN %@) AND (ANY estType.type == %@)", self.companies, figureType];
request.predicate = predicate;
request.resultType = NSDictionaryResultType;
request.returnsDistinctResults = YES;
request.propertiesToFetch = @[@"endCalYear",@"endMonth",@"periodLength"];
request.sortDescriptors =
@[[NSSortDescriptor sortDescriptorWithKey:@"endCalYear" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"endMonth" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"periodLength" ascending:NO]];
NSError *fetchError = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&fetchError];
NSMutableArray *distinctPeriods = results.mutableCopy;
if (fetchError) {
NSLog(@"Error during fetch request:%@", [fetchError localizedDescription]);
} else {
// NSLog(@"results: %@",results);
}
// remove periods for which not all companies have data for estimate type specified
NSString const *endCalYearKey = @"endCalYear";
NSString const *endMonthKey = @"endMonth";
NSString const *periodLengthKey = @"periodLength";
const NSIndexPath *yoyGrowthIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
const NSIndexPath *seqGrowthIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
const NSIndexPath *customGrowthIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
NSMutableIndexSet *indexesForPeriodsToRemove = [NSMutableIndexSet indexSet];
[distinctPeriods enumerateObjectsUsingBlock:^(NSDictionary *estPeriodDict, NSUInteger idx, BOOL *stop) {
NSNumber *endCalYear = estPeriodDict[endCalYearKey];
NSNumber *endMonth = estPeriodDict[endMonthKey];
NSNumber *periodLength = estPeriodDict[periodLengthKey];
NSPredicate *predicate = [[NSPredicate alloc]init];
UITableView *tableView = [self tableView];
if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) {
// company predicate:
predicate = [NSPredicate predicateWithFormat: @"ANY estimateType.type == %@ "
"AND SUBQUERY(estimatePeriod, $x, $x.endCalYear == %@ AND $x.endMonth == %@ AND $x.periodLength == %@ AND ANY $x.estType.type == %@).@count > 0",
figureType,
endCalYear, endMonth, periodLength, figureType];
} else if ( [[tableView indexPathForSelectedRow] isEqual:yoyGrowthIndexPath] ) {
predicate = [NSPredicate predicateWithFormat: @"ANY estimateType.type == %@ "
"AND SUBQUERY(estimatePeriod, $x, $x.endCalYear == %@ AND $x.endMonth == %@ AND $x.periodLength == %@ AND ANY $x.estType.type == %@).@count > 0"
"AND SUBQUERY(estimatePeriod, $x, $x.endCalYear == %i AND $x.endMonth == %@ AND $x.periodLength == %@ AND ANY $x.estType.type == %@).@count > 0",
figureType,
endCalYear, endMonth, periodLength, figureType,
endCalYear.integerValue - 1, endMonth, periodLength, figureType];
} else if ( [[tableView indexPathForSelectedRow] isEqual:seqGrowthIndexPath] ) {
// TODO: rewrite
predicate = [NSPredicate predicateWithFormat: @"ANY estimateType.type == %@ "
"AND SUBQUERY(estimatePeriod, $x, $x.endCalYear == %@ AND $x.endMonth == %@ AND $x.periodLength == %@ AND ANY $x.estType.type == %@).@count > 0",
figureType,
endCalYear, endMonth, periodLength, figureType];
} else {
[NSException raise:NSInternalInconsistencyException format:@"TableView: Invalid selection state in section 0 (NSIndexPath: %@)",super.tableView.indexPathForSelectedRow];
}
NSArray *companiesWithDataForPeriod = [self.companies filteredArrayUsingPredicate:predicate];
NSLog(@"type: %@, period: %@/%@(%@), companies: %@", figureType, endCalYear, endMonth, periodLength,[companiesWithDataForPeriod valueForKey:@"contrSymbol"]);
// mark periods which are not defined for all companies for removal (from display):
if ( companiesWithDataForPeriod.count < self.companies.count ) [indexesForPeriodsToRemove addIndex:idx];
}]; // end block
[distinctPeriods removeObjectsAtIndexes:indexesForPeriodsToRemove];
return distinctPeriods;
}