在逗号分隔值文件中搜索值

时间:2012-12-18 13:08:18

标签: iphone csv nsstring full-text-search nsautoreleasepool

您好我已经实现了以下方法来搜索csv文件中的值:

- (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.上述方法在一个单独的线程中调用,因此我们必须在一个单独的自动释放池中处理它。

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.

2 个答案:

答案 0 :(得分:1)

最后我的问题解决了!! 我使用以下代码来优化内存:

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

答案 1 :(得分:0)

使用内存映射,这样您就不必一次加载所有内容。
请参阅:Substitute for NSData deprecated dataWithContentsOfMappedFile

OR

将文件拆分为几个较小的文件