Xcode 9 - Can't Find .sqlite database on simulator folder

Kimberly Life Source

So a few months ago i was using the same method to find my database. Once the iPhone 6s simulator launches, my .sqlite gets copied to my documents folder. there i was able to edit the database

But after installing a new copy of macOS, i had to re-install xcode again. which i am not sure if i downloaded a new version or the same one.

My issue:

I am doing some local experiments with the SQLite database, means after i finish i must copy the database to a flash drive, but now that it disappears i don't know what to do

This was and my current method of getting the docs folder of the running simulator:

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
print("Docs Path: \(documentsPath)")

But after running the app, not sure if the databases were copied but all i see is an empty "Documents" folder..

sim

And for the reference here's how i got the databases in my project:

sim2

i feel like i am the only one facing this issue and its so frustrating. Please help!

EDIT:

this is how i use the database:

 let path = Bundle.main.path(forResource: "rawand", ofType: "sqlite")
    do {
        let db = try Connection(path!)
    } catch {
        print(error)
    }
swiftxcode

Answers

answered 3 months ago Scriptable #1

You need to use the FileManager class to copy the resource from the bundle to the Documents directory.

    let fileManager = FileManager.default
    guard let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    guard let sourceFilePath = Bundle.main.url(forResource: "test", withExtension: "txt") else { return }
    let destinationPath = documentsPath.appendingPathComponent("test.txt")

    print("[INFO] - destinationPath: \(destinationPath.absoluteString)")

    if !fileManager.fileExists(atPath: destinationPath.path) {
        print("[DEBUG] - File DOES NOT exist")
        do {
            try fileManager.copyItem(at: sourceFilePath, to: destinationPath)
        } catch {
            print(error)
        }
    } else {
        print("[DEBUG] - File DOES exist")
    }

I have tested this code in the simulator with a sample txt file (test.txt) and it is working.

comments powered by Disqus