I have this function that initializes a database (basically copied from the documentation of SQLite.swift).
func createDB() {
let paths = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
let documentsDirectory = paths[0]
print(documentsDirectory)
do {
// creates new if it doesnt exist
let db = try Connection("\(documentsDirectory)/dbtest.sqlite3")
} catch {
print(error)
}
}
Right now I just have it within the @main
struct.
@main
struct MyApp: App {
...
createDB()
}
In the SwiftUI world, how would you create a global DB object that I can use throughout a session (user having the app open)? Or is creating it here fine?