问题
I'm working with SQLite.swift. In the document, the path to the database is:
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first!
but i want to import and use an existing database, so i've dragged my existing database to my keyboard extension folder, and create connection to it with path is:
let path = NSBundle.mainBundle().pathForResource("db", ofType:"sqlite3")
So, i've noticed that the first way, the database will be store in /Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Data/PluginKitPlugin/0BC647E4-26F3-4A1F-8271-CC73C96FD197/Documents
and the second way, the database will be store in the app.
/Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Bundle/Application/E5D9514C-859A-4D4D-A771-A8CE9CDCD3E7/AppName.app/PlugIns/AppNameExt.appex
What's different between these two locations? The second way might increase the app size because it contains the database? And if i want to Archive/Submit my App to the AppStore with existing database, is this the only way?
回答1:
The main difference is that storing the file in the documents folder means you can write (update) it, which is pretty important for a database file. You cannot write to a file in the app bundle.
The usual pattern for using a database in an app is:
- Create a pre-seeded database during development and copy it to the app bundle during building.
- When running, check if the database file exists and is up-to-date in the documents folder.
- If not, copy it from the app bundle.
- Open the database in the documents folder and read/write as desired.
来源:https://stackoverflow.com/questions/34609746/what-different-between-store-database-in-different-locations-in-ios