EDIT:
As asked by Maddy I am including the code that "opens" the file:
I deleted an app and run it again via Xcode. Before there were no files in the documents directory so I checked and since there were no files I could copy my 32 bytes sqlite file with my tables in it from the main bundle.
The problem is that now after I deleted it, it creates a zero byte valid sqlite file in the directory folder with no tables in it when it runs if there's no file in the documents directory. So in the appdelegate when I check for the file it says correctly that is there though is not the file I want. If I manually go to iTunes in the device and delete the file it copies the right file.
This happens both in the simulator and in the device. Any ideas what can I be doing wrong. Please find below a piece of code from the app delegate: Xcode 10.1 and swift 4.2 syntax:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Fabric.with([Crashlytics.self])
let sourcePath = Bundle.main.path(forResource: "mydb", ofType: "db")!
let fileManager = FileManager.default
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let destinationPath = doumentDirectoryPath.appendingPathComponent("mydb")
if !fileManager.fileExists(atPath: destinationPath) {
do {
try fileManager.copyItem(atPath: sourcePath, toPath: destinationPath)
}
catch
{
os_log("error copying database")
Crashlytics.sharedInstance().recordError(error)
print(error)
}
}
return true
}
code that opens the file
import Foundation
import SQLite
import os.log
var connected: Bool
//MARK: - private attributes
var db: Connection?
//MARK: - constructor
init() {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
do {
db = try Connection("\(path)/mydb.db")
connected = true
}
catch {
connected = false
}
}
many thanks in advance. This is really puzzling me.