[英]Where should I store the SQLite DB for my iPhone app?
I have several iOS apps on the market and in all of them I have a small SQLite database file connected to the app to provide the user with my data. 我在市场上有几个iOS应用程序,在所有这些应用程序中,我有一个连接到应用程序的小型SQLite数据库文件,为用户提供我的数据。 Once installed the user customizes this DB by changing certain options.
安装后,用户可以通过更改某些选项来自定义此DB。
Until now, all of these apps store the DB in NSDocumentsDirectory
. 到目前为止,所有这些应用程序都将数据库存储在
NSDocumentsDirectory
。 After submitting my last update, it was rejected for storing data in the wrong location. 提交我的上次更新后,它被拒绝将数据存储在错误的位置。 For the apps rejected I changed the storage location to
NSCachesDirectory
. 对于拒绝的应用程序,我将存储位置更改为
NSCachesDirectory
。 I am now afraid that the cache will get cleared and erase the users customizations. 我现在担心缓存将被清除并删除用户自定义。 The app would still function, the DB would get recreated, but all changed tables would be reset, thereby upsetting users.
该应用程序仍然可以运行,数据库将被重新创建,但所有更改的表都将被重置,从而扰乱用户。
Where should this type of database file be stored? 这种类型的数据库文件应该存储在何处? Was I right to put it in the Docs dir?
我把它放在Docs目录中是对的吗? According to Apple, the Docs dir is for "Critical Data" which is either user generated data or data needed for proper operation of the app.
根据Apple的说法,Docs目录是针对“关键数据”,它是用户生成的数据或正确运行应用程序所需的数据。 I feel that it falls under that category, persisting my users settings is proper operation, but who wants to await an appeal?
我觉得它属于那个类别,坚持我的用户设置是正确的操作,但谁想等待上诉?
Apple's file system programming guide for iOS describes the (application_home)/Library
path as a place to "create custom subdirectories for files you want backed up but not exposed to the user". Apple的iOS文件系统编程指南将
(application_home)/Library
路径描述为“为要备份但未向用户公开的文件创建自定义子目录”的位置。 The (application_home)/Documents
path is described as "the contents of this directory can be made available to the user through file sharing." (application_home)/Documents
路径被描述为“该目录的内容可以通过文件共享提供给用户”。
Your databases don't sound like documents, they sound like private caches that users shouldn't know about. 您的数据库听起来不像文档,它们听起来像用户不应该知道的私有缓存。 I recommend you create a directory such as
(application_home)/Library/Database
and save files there. 我建议您创建一个目录,如
(application_home)/Library/Database
并在那里保存文件。 The cache path you mention doesn't sound like the best option to me. 你提到的缓存路径听起来不是我的最佳选择。
You should really review the entire file system programming guide for iOS before submitting another app. 在提交另一个应用程序之前,您应该查看iOS的整个文件系统编程指南 。
6 year update: 6年更新:
Storing the data in the NSCachesDirectory
caused a bug, as expected, that the users customized data would erase whenever the device felt like it. 将数据存储在
NSCachesDirectory
导致错误,正如预期的那样,只要设备感觉到,用户自定义数据就会被删除。 After many many attempts at arguing the point with Apple app review, I was able to finally post an update where the data is now stored in a custom directory in the NSDocumentsDirectory
(as it should be) which solved all of the aforementioned issues. 经过多次尝试与Apple应用程序评论争论点后,我终于发布了一个更新,其中数据现在存储在
NSDocumentsDirectory
中的自定义目录中(应该如此),这解决了所有上述问题。
If your database indeed contains user-generated content, then the Documents directory is its proper location. 如果您的数据库确实包含用户生成的内容,则Documents目录是其正确的位置。
However, you mention that you are persisting "your users settings"... If the data you are persisting are more akin to settings or preferences, then they should reside in the Defaults subsystem. 但是,您提到您持久保存“用户设置”...如果您持久存储的数据更类似于设置或首选项,则它们应驻留在默认子系统中。 See the documentation for NSUserDefaults .
请参阅NSUserDefaults的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.