iOS Data Storage best practices
This articles is a cheatsheet on organized and optimized data storage for iOS app.
Where can I store application data?
There are five locations to store your application files:
- iCloud drive
- Documents
- Application support
- Caches
- Temporary
How to choose proper storage?
What should I know about each file storage location?
Each file storage location has its own purpose and things to be considered.
iCloud
Files stored in iCloud are accessible across all users devices. Files stored in iCloud are also cached locally on the device and are automatically uploaded to iCloud. Files uploaded to iCloud are subjected to conflicts, therefore file coordination is required and is a developers responsibility.
Documents
Documents folder is accessible by the user. Files can be edited or even removed from ouside the application (e.g. using Files app). Documents directory supports “Open in place”. Documents also support “File sharing enabled”. Files in Documents directory are subject to Storage settings management.
Application support
If the file is required for your application but should not be accessible by the user (e.g. Data Base), you should store it in Application support folder. Files stored in Application Support directory are not removed by the system. Files stored in Application Support directory are reported in “Documents and data”. Files stored in Application Support directory are included in iCloud and iTunes backup. You can prevent backing file up using NSURLIsExcludedFromBackupKey
Caches
Caches directory should contain only purgeable or transient content. Files stored in Caches directory are not included in backups. Files stored in Caches can be deleted by iOS (e.g. in case of low storage capacity) but only when your app is not running. Files in your caches directory are not reported in “Documents and data”.
Temporary
Name says it all. Temporary directory is for transient files. Files stored in Temporary directory are not expected to live any extended period of time. The file should be removed from temporary directory as soon as you finished to work with it.