gpt4 book ai didi

objective-c - 仅在参数更改时处理实例方法中的代码

转载 作者:行者123 更新时间:2023-11-28 22:46:38 25 4
gpt4 key购买 nike

我有一些方法需要一些时间来处理获取数据、执行计算然后返回结果,例如这个实例方法返回一个基于三个参数的数组:

-(NSArray*)periodsForCompanies:(NSArray*)companies figureType:(NSString*)figureType
actualValuesOnly:(BOOL)actualValuesOnly
{.. };

因为这个方法可以从同一个类用相同的参数调用很多次并且需要一些时间才能完成,我想优化我的代码以避免每次都完全执行这个方法的代码。

我如何将之前的参数(和/或结果)存储在方法中,以便能够将当前参数与之前的参数进行比较,并决定是否需要再次执行代码?在这种情况下,“最佳做法”是什么?

我的理解是,通常方法中的所有变量在调用方法时都会重置为零和 nil。

编辑

根据要求,我添加了一个代码示例:

- (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;
}

最佳答案

您需要进行某种缓存,实际上您的参数可能会有很大差异,因为您有公司数组作为参数。如果您使用大量组合调用此方法,可能会导致使用大量内存。

我会将其更改为仅传递单个公司,这样缓存会更小,而不是在最坏的情况下保留所有公司组合。

要将数据保存在缓存中,您可以制作一些 NSMutableDictionary 属性或静态值(记得在某处释放/清空它),然后作为键,您可以将这些参数组合在某个对象中或作为简单的字符串。

所以你的方法的内部实现看起来像:

simpleString = .. combined arguments ...
NSArray *result = [dictionary objectForKey:simpleString]
if (result == nil)
{
.. do fetch and put result in result
[dictionary setObject:result forKey:simpleString];
}
return result;

关于objective-c - 仅在参数更改时处理实例方法中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13070334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com