Looking for best way to load/create large array

Discussion in 'iOS Programming' started by 99miles, Jan 27, 2009.

  1. 99miles macrumors member

    Joined:
    Oct 10, 2008
    #1
    Hi -
    What's the best way to load/create a large array of strings? When in a text file, the file is about 2 MB.

    The contents will never change so I could keep it in my source (but then my app takes forever to compile), I can store it in a text file and load it, then create the array by using newline as a delimiter but [[[NSString stringWithContentsOfFile:filePath] componentsSeparatedByString:mad:"\r\n"] takes about 5 seconds.

    Is there a faster/better way?
    Thanks
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    Why don't you try creating an XML file? Then you just need:

    NSArray *newMyArray = [[NSArray alloc] initWithContentsOfFile: myXMLFile];

    I'm pretty sure there is a method to take the TXT file that you have now and turn it into an XML for you.
     
  3. bnut macrumors newbie

    bnut

    Joined:
    Nov 16, 2008
    #3
    In general if you need speed go lower level, there are small overheads with Objective C methods. I may be wrong, but I imagine your method call will allocate the NSString for the entire file, perhaps do a representation change on initialisation (this I assume). Then it will parse it again and split it into the substrings, which will have to be allocated as well. If you have an already allocated buffer for a line, then use a scanner to read each line, then create the NSString from the data with the appropriate encoding, I would assume it to be faster... however probably not future proof or particularly nice OO. :p I'd expect 2MB to take a while anyway.
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    Seconding bnut; for speed you really should compile your data in an array. If you keep it in a static method in its own class it shouldn't need to be compiled more than once (Xcode will cache the compiled code) so won't cause much of a slow down when compiling.
     
  5. nomar383 macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #5
    I'm in the same type of situation. I have about 2-5MBish worth of static text that will need to be stored in some fashion. What is the best way? An SQL database, XML, plist? Should I really just hardcode it into its own method (in an array)? I haven't been able to find much information on this type of thing online
     
  6. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #6
    Depends how you want to access it, but for speed your best bet is going to be either SQL or hardcoding.
     
  7. nomar383 macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #7
    Hard coding is very very tempting as I don't know SQL that well. Will it affect the overall load time of the app to do it this way?
     
  8. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #8
    Load time for hard coded will be far faster than XML/plist since there's no processing to do to get the data into a useful state :)
     
  9. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #9
    data tables

    I had an idea for a few apps, but needed to know what the best structure was for what I wanted to do. As a newbie, I had a long journey to work things out, but am quite methodic in my approach, and didn't mind too much how long it took me. So looked at SQLite, plist and resident array solutions I found in some terrific tutorials on the web.

    Hard coding the data into an array in the app was the fastest and simplest method, but was a bit of a pain managing the amount of data entries I was working up to. A plist made things much easier to manage, but the added setup and table management code made a fair loading difference when you have over 1000 titles and related details. The same can be said for a SQLite db. [Incidentally, I use SQLite manager in Firefox.]

    So, the conclusion I came to was for apps with just fifty or less entries, or picker/selection lists, I would use a resident array or maybe a plist, for anything more than that I would use SQLite.

    Hope my ramble helps.

    Boyplunder
     

Share This Page