Integrated SQLITE Database, below is my code which I have written,
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.copyDatabaseIfNeeded()
return true
}
func copyDatabaseIfNeeded() {
let fileManager = FileManager.default
let dbPath = getDBPath()
var success: Bool = fileManager.fileExists(atPath: dbPath!)
if !success {
let defaultDBPath = URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent(APPLICATION_DB).absoluteString
success = ((try?fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath!)) != nil)
if !success {
print("Failed to create writable database!")
}
}
}
func getDBPath() -> String? {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDir = paths[0]
return URL(fileURLWithPath: documentsDir).appendingPathComponent(APPLICATION_DB).absoluteString
}
It always print below output,
- Tried clean + Build Project
- Uninstall the application, reinstall it
- Remove derived data
none of the above has worked.
Also, my sqlite file is there in project target -> Build Phases -> Copy Bundle Resources : below screenshot for reference.
I am not sure whether hierarchy of my file matters or not, attaching screenshot of that as well.
Can anyone help me why I am getting issue in fetching file path of my database file?