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 9022447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:30:45+00:00 2026-06-16T05:30:45+00:00

Hi I have implemented the following method for searching a value in a csv

  • 0

Hi I have implemented the following method for searching a value in a csv file:

- (void)recordsForValue:(NSString*)searchedValue {


NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

NSError*  err;

    NSString *csvFilePath = //path of csv file
    NSString *csvString = nil;
    csvString = [[NSString alloc]initWithContentsOfFile:csvFilePath encoding:NSASCIIStringEncoding error:&err];
    csvFilePath = nil;
    NSRange searchRange = NSMakeRange(0,csvString.length);
    NSRange foundRange;
    int i=0;

    locationRecordsArray = [NSMutableArray array];

    while (searchRange.location <= csvString.length) {
        NSString *sFinalResult = nil;

        searchRange.length = csvString.length-searchRange.location;
        foundRange = [csvString rangeOfString:searchedValue options:NSCaseInsensitiveSearch range:searchRange];
        if (foundRange.location != NSNotFound) {
            i++;

            NSRange endDivRange;
            NSRange startRange;

            endDivRange.location = foundRange.length + foundRange.location;
            endDivRange.length   = [csvString length] - endDivRange.location;
            endDivRange = [csvString rangeOfString:@"\n" options:NSCaseInsensitiveSearch range:endDivRange];

            if (endDivRange.location != NSNotFound)
            {
                // Tags found: retrieve string between them
                foundRange.location += foundRange.length;
                foundRange.length = endDivRange.location - foundRange.location;
            }

            startRange.location = 0;
            startRange.length = foundRange.location;
            NSString* sTemp = nil;
            sTemp = [[NSString alloc]initWithString:[csvString substringWithRange:startRange]];

            startRange = [sTemp rangeOfString:@"\n" options:NSBackwardsSearch range:startRange];
            [sTemp release];
            if (startRange.location != NSNotFound)
            {
                // Tags found: retrieve string between them
                foundRange.location = startRange.location;
                foundRange.length =   endDivRange.location - foundRange.location;

                sFinalResult = nil;
                sFinalResult = [[NSString alloc]initWithString:[csvString substringWithRange:foundRange]];
                if(sFinalResult != nil)
                    [locationRecordsArray addObject:sFinalResult];
                [sFinalResult release];

            }

            // found an occurrence of the substring! do stuff here
            searchRange.location = foundRange.location+foundRange.length;
        } else {
            // no more substring to find
            break;
        }
    }
    [csvString release];


    [self insertLocationRecordsInDatabase]; // insert records of locationArray

[self performSelectorOnMainThread:@selector(showScanningView) withObject:nil waitUntilDone:NO];
[pool drain];
}

1.The above method is called in a separate thread, so we have to process it in a separate autorelease pool.

2. The 'searchedValue' is a value to be searched in the CSV file string.

3. Whenever we get a location for the value searched in the csvString, we add the complete line of string in an array.The line
is extracted by searching for newline (i.e \n) character.
4. This code is running fine if the length of csvString is small. But in case of a very large csvString, the app is crashing with low
memory warning. I checked in Instruments. The problem is with some
String variable which is reaching the size of 100s of MB and the app
is closing because of low memory.But I am not able to solve how to
optimise the memory of my string variables.Please give some
solution.
  • 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-16T05:30:47+00:00Added an answer on June 16, 2026 at 5:30 am

    Finally my problem solved!!
    I used the following code to optimise the memory:

    - (void)recordsForLocationValue:(NSString*) searchedValue
     {
    
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    
        NSError*  err;
    
             NSString *csvFilePath = //path of csv file
            NSMutableString *csvString = [NSMutableString stringWithContentsOfFile:csvFilePath encoding:NSASCIIStringEncoding error:&err];
            csvFilePath = nil;
            NSRange searchRange = NSMakeRange(0,csvString.length);
            NSRange foundRange;
            int i=0;
            NSString* sTemp = nil;
    
            locationRecordsArray = [[NSMutableArray alloc]init];
    
            while ([csvString length] > 1) {
    
                searchRange.length = csvString.length-searchRange.location;
                foundRange = [csvString rangeOfString: searchedValue
     options:NSCaseInsensitiveSearch range:searchRange];
                if (foundRange.location != NSNotFound) {
                    i++;
    
                    NSRange endDivRange;
                    NSRange startRange;
    
                    endDivRange.location = foundRange.length + foundRange.location;
                    endDivRange.length   = [csvString length] - endDivRange.location;
                    endDivRange = [csvString rangeOfString:@"\n" options:NSCaseInsensitiveSearch range:endDivRange];
    
                    if (endDivRange.location != NSNotFound)
                    {
                        // Tags found: retrieve string between them
                        foundRange.location += foundRange.length;
                        foundRange.length = endDivRange.location - foundRange.location;
                    }
    
                    startRange.location = 0;
                    startRange.length = foundRange.location;
                    sTemp = [csvString substringWithRange:startRange];
                    startRange = [sTemp rangeOfString:@"\n" options:NSBackwardsSearch range:startRange];
                    if (startRange.location != NSNotFound)
                    {
                        // Tags found: retrieve string between them
                        foundRange.location = startRange.location;
                        foundRange.length =   endDivRange.location - foundRange.location;
    
                        sTemp = [csvString substringWithRange:foundRange];
                        if(sTemp != nil)
                            [locationRecordsArray addObject:sTemp];
                    }
    
                    // found an occurrence of the substring! do stuff here
                    searchRange.location = foundRange.location+foundRange.length;
    
                    [csvString deleteCharactersInRange:NSMakeRange(0, searchRange.location-1)];
    
                    searchRange.location = 0;
                }
                else {
                    NSLog(@"no more substring to find");
    
                    // no more substring to find
                    break;
                }
            }
    
    
            [self insertLocationRecordsInDatabase];
    
        [self performSelectorOnMainThread:@selector(showScanningView) withObject:nil waitUntilDone:NO];
        [pool drain];
        [NSThread exit];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have implemented following method in my application. - (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler {
I have a custom scrollView with the following method implemented: - (void) touchesEnded:(NSSet *)touches
I have a form (form2) and I implemented the following PUBLIC method: function ShowInterface(i:integer):boolean;
I have the following method signature: public static void InvokeInFuture(Delegate method, params object[] args)
I have implemented following async blob upload method to upload multiple blocks. var container
i have implemented the following picklist: <p:pickList id=pickList value=#{reportConfiguratorBean.dualListVars} var=cRVariable itemValue=#{cRVariable} itemLabel=#{cRVariable.varName} converter=#{cRImageTypeConverter} immediate=true
I have a little Problem, i have implemented the following method to open an
I have implemented the following NSOperation, to draw N custom views - (void)main {
I have an iPhone application and I need to implement the following method: +(UITextView
I have implemented the following set up (after being requested): slideshow of images changing,

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.