1

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.

Juanjo
  • 670
  • 7
  • 17
  • 1
    Show the code that attempts to open the database file from the Documents folder. – rmaddy Dec 04 '18 at 03:29
  • 1
    are you accidentally opening a file with a mode that is creating it? – Grady Player Dec 04 '18 at 03:35
  • @maddy I just got this from the source code: - filename: The location of the database. Creates a new database if /// it doesn’t already exist (unless in read-only mode). – Juanjo Dec 04 '18 at 21:32

1 Answers1

0

I am using Sqlite.swift and here is the constructor to the Connection method:

/// Initializes a new SQLite connection.
///
/// - Parameters:
///
///   - location: The location of the database. Creates a new database if it
///     doesn’t already exist (unless in read-only mode).
///
///     Default: `.inMemory`.
///
///   - readonly: Whether or not to open the database in a read-only state.
///
///     Default: `false`.
///
/// - Returns: A new database connection.
public init(_ location: SQLite.Connection.Location = default, readonly: Bool = default) throws

So it creates the database (blank with no tables) if does not exists. So I changed the code in the edited question to this:

//MARK: - constructor
init() {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    do {
        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.db")
        if !fileManager.fileExists(atPath: destinationPath) {
            try fileManager.copyItem(atPath: sourcePath, toPath: destinationPath)
        }

        db = try Connection("\(path)/mydb.db")
        connected = true
    }
    catch {
        connected = false
    }
}

and it works. Thanks a lot for the clues.

Juanjo
  • 670
  • 7
  • 17