我们应该如何决定使用documentDirectory还是applicationSupportDirectory? [英] How should we decide whether to use documentDirectory or applicationSupportDirectory?
问题描述
我们计划存储以下用户文件
We plan to store the following user files
- SQLite文件
- 图像文件
- 音频文件
我们还为用户提供了一个选项,可以将上述文件上传和下载到第三方云存储中.
We also provide an option for users, to upload and download the above files, to a 3rd party cloud storage.
我想知道我们应该使用
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
或
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
出于上述文件存储目的?
for the above file storage purpose?
推荐答案
从历史上看,我们使用"Documents"文件夹,但如今,"Application Support"目录是此类文件的最佳存放位置.
Historically we used the "Documents" folder, but nowadays the "Application Support" directory is the best place for files like this.
将用户数据放入 Put user data in 将应用程序创建的支持文件放入 Put app-created support files in the 请记住,默认情况下会备份 Remember that files in 将临时数据放入 Put temporary data in the 将数据缓存文件放入 Put data cache files in the 另请参见 iOS存储最佳做法视频. 如果使用 If using the 这篇关于我们应该如何决定使用documentDirectory还是applicationSupportDirectory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Documents/
中.用户数据通常包括您可能希望向用户公开的任何文件-您可能希望用户创建,导入,删除或编辑的所有文件.对于绘图应用程序,用户数据包括用户可能创建的任何图形文件.对于文本编辑器,它包括文本文件.视频和音频应用程序甚至可能包含用户下载以供以后观看或收听的文件.
Documents/
. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later. Library/Application support/
目录中.通常,此目录包含应用程序用来运行但仍对用户隐藏的文件.此目录还可以包括数据文件,配置文件,模板以及从应用程序捆绑包加载的资源的修改版本.Library/Application support/
directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle. Documents/
和 Application Support/
中的文件.您可以使用 NSURLIsExcludedFromBackupKey
键调用-[NSURL setResourceValue:forKey:error:]
从备份中排除文件.必须将所有可以重新创建或下载的文件排除在备份之外.这对于大型媒体文件尤为重要.如果您的应用程序下载了视频或音频文件,请确保它们不包含在备份中.Documents/
and Application Support/
are backed up by default. You can exclude files from the backup by calling -[NSURL setResourceValue:forKey:error:]
using the NSURLIsExcludedFromBackupKey
key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads video or audio files, make sure they are not included in the backup. tmp/
目录中.临时数据包含您不需要长时间保留的任何数据.完成操作后,请记住删除这些文件,以免它们继续占用用户设备上的空间.当您的应用未运行时,系统会定期清除这些文件;因此,您无法在应用终止后依赖这些文件的持久性.tmp/
directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore, you cannot rely on these files persisting after your app terminates. Library/Caches/
目录中.高速缓存数据可用于需要比临时数据保留更长时间的任何数据,但不能与支持文件一样长.一般而言,应用程序不需要高速缓存数据才能正常运行,但是可以使用高速缓存数据来提高性能.缓存数据的示例包括(但不限于)数据库缓存文件和瞬时的可下载内容.请注意,系统可能会删除 Caches/
目录以释放磁盘空间,因此您的应用程序必须能够根据需要重新创建或下载这些文件.Library/Caches/
directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete the Caches/
directory to free up disk space, so your app must be able to re-create or download these files as needed. .applicationSupportDirectory
,建议您使用.applicationSupportDirectory
, I’d suggest you use url(for:in:appropriateFor:create:)
with create
set to true
:let folderURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)