1

こんにちは私は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.
4

2 に答える 2

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];
}
于 2012-12-21T07:22:35.477 に答える
0

一度にすべてをロードする必要がないように、メモリ マッピングを使用します。
参照:非推奨の NSData の代替 dataWithContentsOfMappedFile

また

ファイルをいくつかの小さなファイルに分割する

于 2012-12-18T13:11:12.647 に答える