Right place to store the application data in IOS

2019-02-19 19:28发布

问题:

Currently i was saving my application data (Media) to the CacheDirectory i.e

/var/mobile/Applications/BEAFC76C-C450-4A3A-9765-A0385A9580F3/Library/Caches

and things were going fine. But recently i got a bug report that the application data has been deleted. When i searched over it, i got this Apple Doc. According to it, DocumentsDirectory should be the ideal place to store the User/Application data.

Put user data in the /Documents/. User data is any data that cannot be recreated by your app, such as user documents and other user-generated content.

And Cache should not be used to store the User Data that could not be reproduced by the application.

Put data cache files in the /Library/Caches directory. Examples of files you should put in this directory include (but are not limited to) database cache files and downloadable content, such as that used by magazine, newspaper, and map apps. Your app should be able to gracefully handle situations where cached data is deleted by the system to free up disk space.

What should be the ideal place to store it.

EDIT:

I have an application that allows user to store Videos and Photos in the application. For that i used CacheDirectory. But i am getting bug reports that the Data (Videos/Photos) is getting deleted. What conclusion i draw is that the data is being getting delete by the Device itself in order to provide space.

Secondly i also wanna give the iTunes sharing function. So only the particular files has to be stored in the DocumentsDirectory. Some files can never be exposed and some has has to be shared. What should be the ideal way to store the files.

回答1:

Library/Application Support Folder is the folder you should be using.

This directory doesn't always exist, and thus you may need to create it. You can enable or disable weather you want to backup this data with itunes or not.

This data is not accessible even if you enable file sharing. Only data that you put in Document directory would be shared with iTunes sharing, so you can still protect your data and get it backed up as well. Apple's documentation



回答2:

Use Documents (NSDocumentDirectory) for files you wish to share via iTunes.

Use Application Support (NSApplicationSupportDirectory) for files you wish to hide from the user but still be backed up and never deleted by the OS.



回答3:

Starting iOS 5, Apple says that it's no longer a good thing to save all kind of files in Documents Directory - if you do that, your app will be rejected for sure because this folder is backed up to iTunes & iCloud, unless otherwise specified.

It says that we should save files into Caches or Tmp Directory - these won't be backed up, but it's not a good thing to do because files from these directories can disappear if low memory happens.

So I think the best think to do is to save the important files that you need all the time in your app into Documents Directory and mark them not to be backed up, like this.