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.
Finally my problem solved!!
I used the following code to optimise the memory: