gpt4 book ai didi

ios - 我们应该如何决定是使用 documentDirectory 还是 applicationSupportDirectory?

转载 作者:行者123 更新时间:2023-11-28 18:46:16 25 4
gpt4 key购买 nike

我们计划存储以下用户文件

  • SQLite 文件
  • 图片文件
  • 音频文件

我们还为用户提供了将上述文件上传和下载到第 3 方云存储的选项。

我在想,我们是否应该使用

FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)

用于上述文件存储目的?

最佳答案

过去我们使用“Documents”文件夹,但现在“Application Support”目录是存放此类文件的最佳位置。

File System Programming Guide

  • Put user data in 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.

  • Put app-created support files in the 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.

  • Remember that files in 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.

  • Put temporary data in the 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.

  • Put data cache files in the 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.

另见 iOS Storage Best Practices视频。


如果使用.applicationSupportDirectory,我建议你使用url(for:in:appropriateFor:create:) create 设置为 true:

let folderURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

关于ios - 我们应该如何决定是使用 documentDirectory 还是 applicationSupportDirectory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com