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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With