I’m developing a simple text editor for iPad.
Instead of Core Data, the app just saves its content to *.txt file in the document folder by calling writeToFile function of NSString. In this way, users can easily transfer files via iTunes. Please, advise me if this approach is bad or inefficient!
How often should the program save its content to the text file? The iOS human guide says that the app should save it’s content “frequently” but it doesen’t give specific seconds.
I read somewhere that the app should save its content every two seconds. If this is correct, do I just need to make one function and call it using NSTimer with repetition mode?
Thank you!
There’s little point in saving “frequently” unless you actually expect to crash. iOS gives you notification of potential app shutdown situations, so I’d just save upon: 1) Receiving a memory warning 2) Termination 3) Moving to background and 4) Whenever the user asks for it (be that via a button, or by navigating away from the editing view). Any saving beyond that is really a waste of resources, as the actual editor isn’t going anywhere, short of crashing out.
The
writeToFilemethod of NSString is fine for plaintext and assuming you’ve got your data as an NSString already. If your data isn’t already inside an NSString, then there are more efficient ways of saving it than converting to an NSString and callingwriteToFile(output streams, for example). Also, if you’re using very large files, you’ll probably want a more incremental approach (i.e. saving from just the point of change to the end). The odds of anyone editing a file large enough on the iPad to need this though, are slim.