Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Once installed the user customizes this DB by changing certain options.

Until now, all of these apps store the DB in 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. 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? 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. I feel that it falls under that category, persisting my users settings is proper operation, but who wants to await an appeal?

like image 975
JerseyDevel Avatar asked Feb 19 '12 04:02

JerseyDevel


People also ask

Where is SQLite database stored iOS?

The database that can be used by apps in iOS (and also used by iOS) is called SQLite, and it's a relational database. It is contained in a C-library that is embedded to the app that is about to use it. Note that it does not consist of a separate service or daemon running on the background and attached to the app.

Where SQLite database is stored?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

Can iOS apps use SQLite?

SQLite is available by default on iOS. In fact, if you've used Core Data before, you've already used SQLite. Core Data is just a layer on top of SQLite that provides a more convenient API.


2 Answers

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". The (application_home)/Documents path is described as "the contents of this directory can be made available to the user through file sharing."

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. 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.

like image 199
bneely Avatar answered Oct 18 '22 03:10

bneely


6 year update:

Storing the data in the NSCachesDirectory caused a bug, as expected, that the users customized data would erase whenever the device felt like it. 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.

like image 2
JerseyDevel Avatar answered Oct 18 '22 05:10

JerseyDevel