BlueChips23
BlueChips23

Reputation: 1951

iOS: Handling downloaded data inside the iOS App

My app downloads some images and creates text files from parsed JSON values. The downloaded images and the content from the text files are then shown to the viewer in a different view controller.

Earlier in my first submission of the app, I got rejected because I completely forgot to handle those downloaded files (which were downloaded into Documents directory) before submitting my app. But ever since, I have doubled down in making sure my app as perfect as possible which means reading and re-reading Data storage guidelines and Human Interface Guidelines so that my app doesn't get rejected when I submit it again.

So I have few questions regarding handling data and memory:

Before: I was saving all the downloaded content from the internet into Documents directory (which I realize now was a mistake). I was then creating a plist file which had the path of the downloaded images and text. So all I had to do in another view controller was to open the plist file, read the images and text file path, and load the content to show it to the user.

I forgot to delete those downloaded files when the view controller closed (although, I was overwriting the existing files in the next app run). The result was, when you launched the app, there was about 15 mb of data already there from the previous session which was never deleted but were overwritten when the user did another search.

After: I have taken these following steps: a) Now I am downloading all my internet data into "tmp" directory. Once the 2nd view controller closes and the user has viewed all the content, I delete all my files including the plist files from "tmp" directory.

b) At app launch time, I also check if there were any remaining files from previous launch (what if the app got terminated). If I find any downloaded files from previous session, I delete them.

c) When the user adds a particular data to his/her favorites (e.g. favorite news article), then I save the corresponding image and text to /Documents/Data directory, and create a Favorites PList file in /Documents directory which stores the path of the user's favorite data. This plist file and the favorite files do not get deleted, unless the user deletes the article from the tableview.

Q 1) Is above a good approach? Should I be saving these content to /Library/Caches or is /tmp good?

Q 2) I am not using iCloud but at the same time, I want to ensure that if the user has any favorites, they should get backed up to the iCloud. Is this okay, or do I really have to use "do not back up" attribute for the favorite files?

EDITS Alan Duncan - Thanks for the memory leaks answer.

I have removed the questions regarding the memory leaks. Regarding data storage questions, they are both related to Apple's Human Interface Guidelines. But if you can answer anyone of those remaining questions, it will be valuable.

Upvotes: 3

Views: 397

Answers (1)

FiletMignon
FiletMignon

Reputation: 53

The documents directory exists for storing files that the user has created himself or that cannot be recreated at a later date, therefor they are backed up on iCloud or when the user makes an iTunes backup. The documents folder will never be cleared unless the user uninstalls the app.

The cache directory exists for storing files that you want to cache on the device but can easily be recreated at a later time, for example downloaded images. The cache folder can automatically be cleared by the OS if the device is running low on free disk space and ofcourse when the user uninstalls the app.

The temp directory exists for temporarily storing files during a session. This folder is automatically cleared when the user closes the app.

You should consider storing the user's favorite articles along with the images in the cache directory since they can always be downloaded again at a later time. Unless you explicitly state that the user should be able to see the full article with images even in offline mode (like the Pocket app), then you should store them in the documents directory along with your plist. The search results should be stored in the temp directory, although I'm not quite sure why you'd want to save search results to disk if the user isn't going to do anything with them?

Upvotes: 2

Related Questions