Where to put SQLite database to keep it from being

2019-04-01 20:11发布

问题:

I'm developing my first OS X application which will be distributed through Mac App Store.

The app makes use of an sqlite database which I am currently keeping on the same location as the rest of app file dependencies.

I'm using Qt Framework to develop the application.

Keeping within the boundaries of App Store rules;

  • Where can I locate the database file for it not to be overridden during app updates?

  • How should I structure the .app folder and all the dependencies?

Thank you.

回答1:

Your question implies that you are writing to the sqlite database, which is a bad idea given the application will be stored in a location (/Applications) that the user may not have write-access to.

What you should do is copy/create the database to a user-writable area (the Documents folder of the app's container will do) upon first use (or when you detect it's not there) and use it from there.

EDIT: In order to get the Documents directory use this code:

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

This will give you the location of the app container Documents directory, not the user's Documents directory when the app is sandboxed:

~/Library/Containers/com.yourdomain.yourapp/Data/Documents