Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextChecker: Memory Issue With Learning Thousands Of Words

I am trying to parse a medical dictionary (.csv file) and then learn all those words through the UITextChecker: learnword method so that the spellchecker approves those medical terms as valid words.

I am calling this method in another thread but the amount of words in the csv file is around 50K.

- (void)parseMyCSVFile{

for (int i = 1; i < [csvContent count]; i++) {
    NSString *learntWord = [NSString stringWithFormat:@"%@",[csvContent objectAtIndex:i]];

    NSString *s = learntWord;
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@" ()\n\""];
    s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];


    if ([UITextChecker hasLearnedWord:s]){

        NSLog(@"skipped");

    }
    else
    {
        [UITextChecker learnWord:s]; // Memory Issue Here
        NSLog(@"learning");
    }

    HUD.detailsLabelText = [NSString stringWithFormat:@"%i of %i",i,[csvContent count]];
}

[self performSelectorOnMainThread:@selector(bgWorkEnded) withObject:nil waitUntilDone:YES];

}

I have applied Instruments Time Profiler and found out that the problem lies in the line where I am learning word inside the loop.

The app tries to load the dictionary till 5000 words (approx) and then crashes.

Any help would be appreciated.

Thanks

like image 448
n.by.n Avatar asked Nov 13 '22 12:11

n.by.n


1 Answers

You must not call UIKit class methods on a background thread--this will result in crashes. Also, you must create an autorelease pool inside your method so that you do not leak any objects. Since UITextChecker must be used on the main thread, I'd recommend only adding a few words at a time, then returning to the run loop so that you don't stall the app. Display a spinner to the user during this process so they know what it's doing.

like image 70
Idles Avatar answered Nov 15 '22 05:11

Idles