What different between store database in different locations in iOS?

会有一股神秘感。 提交于 2019-12-09 01:04:05

问题


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:

  1. Create a pre-seeded database during development and copy it to the app bundle during building.
  2. When running, check if the database file exists and is up-to-date in the documents folder.
  3. If not, copy it from the app bundle.
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!