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