@@ -138,50 +138,14 @@ let db = try Connection("path/to/db.sqlite3")
138138
139139#### Read-Write Databases
140140
141- On iOS, you can create a writable database in your app’s ** Documents **
141+ You can create a writable database in your app’s ** Application Support **
142142directory.
143143
144144``` swift
145- let path = NSSearchPathForDirectoriesInDomains (
146- .documentDirectory , .userDomainMask , true
147- ).first !
148-
149- let db = try Connection (" \( path ) /db.sqlite3" )
150- ```
151-
152- If you have bundled it in your application, you can use FileManager to copy it to the Documents directory:
153-
154- ``` swift
155- func copyDatabaseIfNeeded (sourcePath : String ) -> Bool {
156- let documents = NSSearchPathForDirectoriesInDomains (.documentDirectory , .userDomainMask , true ).first !
157- let destinationPath = documents + " /db.sqlite3"
158- let exists = FileManager.default .fileExists (atPath : destinationPath)
159- guard ! exists else { return false }
160- do {
161- try FileManager.default .copyItem (atPath : sourcePath, toPath : destinationPath)
162- return true
163- } catch {
164- print (" error during file copy: \( error ) " )
165- return false
166- }
167- }
168- ```
169-
170- On macOS, you can use your app’s ** Application Support** directory:
171-
172-
173- ``` swift
174- // set the path corresponding to application support
175- var path = NSSearchPathForDirectoriesInDomains (
176- .applicationSupportDirectory , .userDomainMask , true
177- ).first ! + " /" + Bundle.main .bundleIdentifier !
178-
145+ let path = URL.applicationSupportDirectory
179146// create parent directory inside application support if it doesn’t exist
180- try FileManager.default .createDirectory (
181- atPath : path, withIntermediateDirectories : true , attributes : nil
182- )
183-
184- let db = try Connection (" \( path ) /db.sqlite3" )
147+ try FileManager.default .createDirectory (atPath : path, withIntermediateDirectories : true , attributes : nil )
148+ let db = try Connection (dbURL.appendingPathComponent (" db.sqlite3" ).path )
185149```
186150
187151#### Read-Only Databases
@@ -201,12 +165,6 @@ let db = try Connection(path, readonly: true)
201165> it to a writable location _ before_ establishing a connection (see
202166> [ Read-Write Databases] ( #read-write-databases ) , above, for typical, writable
203167> locations).
204- >
205- > See these two Stack Overflow questions for more information about iOS apps
206- > with SQLite databases: [ 1] ( https://stackoverflow.com/questions/34609746/what-different-between-store-database-in-different-locations-in-ios ) ,
207- > [ 2] ( https://stackoverflow.com/questions/34614968/ios-how-to-copy-pre-seeded-database-at-the-first-running-app-with-sqlite-swift ) .
208- > We welcome changes to the above sample code to show how to successfully copy and use a bundled "seed"
209- > database for writing in an app.
210168
211169#### In a shared group container
212170
0 commit comments