Search code examples
swiftperformancedata-storage

Should you use external files to store large data?


I am making an app that uses many large texts that are predefined. At the moment, I am storing them in a RTF file and loading them from there to separate code from content. If the rtf file will have around 1500-2000 lines and it is loaded and split several times when the app launches, will the app need very much time to load and should I define the content in the source-code?

Thanks for your help!


Solution

  • I definitely would not be inclined to put 2000+ lines of text in the source code itself. A separate file (or database or whatever) makes a lot more sense.

    Regarding whether the app will require much time to load it, you should simply benchmark it (e.g. put let start = CFAbsoluteTimeGetCurrent() before the load and let elapsed = CFAbsoluteTimeGetCurrent() - start after, and see how long it takes.

    By the way, if iOS, make sure you do this on an actual device, not the simulator to get accurate times. But usually loading a small file like this will be inconsequential regarding time required (e.g. it’s likely still going to be smaller than many images).

    I just tested loading a fairly heavily marked up rtf (every word a different color), which was 250kb. Using the NSAttributedString initializer that takes a file URL, while it was an order of magnitude slower than a plain text file (same text, no markup), it still only took 70ms on my iPhone. But clearly, you should benchmark your own file.