Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:16:51+00:00 2026-06-13T15:16:51+00:00

I have some methods which take some time to process to fetch data, perform

  • 0

I have some methods which take some time to process to fetch data, perform calculations and then the return the result, for instance this instance method returns an array based on three parameters:

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

As this method could be called many times with the same parameters from the same class and takes some time to finish, I would like to optimize my code in order to avoid that the code for this method is fully executed each time.

How could I store the previous parameters (and/or results) inside the method to be able to compare the current parameters to the previous parameters and decide if the code needs to be executed again? What is "best practice" in this case?

My understanding is that usually all variables inside he method are reset to zero and nil when the method is called.

Edit

By request I have added a code sample:

- (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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:16:52+00:00Added an answer on June 13, 2026 at 3:16 pm

    You need to make some kind of caching, actually your parameters can vary very much because you have companies array as an argument. That might cause a lot of memory will be used if you call this method using a lot of combinations.

    I would change it to pass only single company so the cache would be smaller instead of keeping all combinations of companies in the worst case.

    To keep data in cache you can make some NSMutableDictionary property or static value (remember to release/empty it somewhere), then as a key you would keep these arguments combined in some object or as a simple string.

    So your internal implementation of your method would look like:

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Often we have to use some of the methods which don't return anything useful.
I have two files called file_utils.h and file_utils.cpp which contain some methods and variables
I have a JQuery control which has some private methods. I would like to
I have a method myButtonAction which performs some heavy calculations, which I need to
I'm currently creating a process for our business which takes some data from a
I'm have some difficulties here, I am unable to successfully call a method which
I have a method which returns some xml in a memory stream private MemoryStream
I have a method which takes a list and do some processing on it
I have some code which asserts an exception is thrown when a method is
I have some code, which uses the ExecuteStmt method on the Axapta Object when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.