简体   繁体   中英

Best way to load a Dictionary into an Application? iOS

I created a game that involves the use of a dictionary. It has about 73000 words. However, after the review process the app got rejected. Here is the review comments:

"We found that your app crashed on an iPad running iOS 6.1.3" "The app crashes when tapping the play button." "Your app may encounter this issue if it is using too much memory."

I am stumped. No problem with the app on my iPhone running 6.1.3 or my iPad running 6.1.3. So I am assuming it crashed on an iPad mini. Is there a more efficient way/better place to load a dictionary into memory? This is how I currently do it after the play button is pressed. Thanks in advance!

    NSString *filePath = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"dictionary"] ofType:@"txt"]; //set where to get the dictionary from
    NSData* data = [NSData dataWithContentsOfFile:filePath]; //pull the content from the file into memory
    NSString* string = [[[NSString alloc] initWithBytes:[data bytes]
                                                 length:[data length] 
                                               encoding:NSUTF8StringEncoding] autorelease];//convert the bytes from the file into a string
    NSString* delimiter = @"\n";//split the string around newline characters to create an array
    currentDict = [string componentsSeparatedByString:delimiter];
    [currentDict retain];

It doesn't seem to me as though 73,000 words would place a strain on memory, and it may be something else entirely. Run Instruments, use the allocations tool to monitor what's happening with your memory. Identify if it really is this dictionary that's at fault.

That said, the strategy you're using isn't great for a number of reasons (including the one you're suggesting it might be). There are a number of alternate approaches that you could use to let you get at the part of the dictionary you need at any given time versus your current approach, which as far as I can tell resides completely in memory all the time.

A simple solution might be to convert to an NSDictionary and load that from file. Another more complex but more efficient yet solution would be to roll out a pre-populated CoreData database (there are a number of existing solutions if you snoop around github for example).

That seems like an exceptionally slow and memory intensive way to store 70k words.

I would recommend instead storing the whole dictionary in a small preloaded SQLite database, or at least a binary plist file - the database is really a better choice though as you can run searches without all the contents being in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM