mercredi 22 avril 2015

Database present in simulator but not on device build (Copying files from App Bundle to Documents Directory in Swift)

I've got a problem that I'm not able to find an answer to.

I have an app that has a sqlite database (called "myDatabase.sqlite"). In order to get this to work in the simulator build, I found the path for it by using the following code:

        let fileManager = NSFileManager.defaultManager()
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

    let docsDir = dirPaths[0] as! String

    databasePath = docsDir.stringByAppendingPathComponent("myDatabase.sqlite")

    let database = FMDatabase(path: databasePath as String)

This works in the simulator, but I had to physically put the file in that path.

For on-device testing, I'd like this to transfer with the bundle. I have that same database copied into the app bundle (oops, I don't have enough reputation to post images, but it's underneath one of my view controllers in the list of files in the app bundle).

When I run the build on the device, though, I get the following readout from the console:

Opened database at /var/mobile/Containers/Data/Application/64ECF088-1AA8-4B9F-AF65-4E36EF10AF9C/Documents/myDatabase.sqlite 2015-04-20 11:39:15.991 TestApp[15882:2776428] DB Error: 1 "no such table: cards" 2015-04-20 11:39:15.992 TestApp[15882:2776428] DB Query: SELECT * FROM cards 2015-04-20 11:39:15.992 TestApp[15882:2776428] DB Path: /var/mobile/Containers/Data/Application/64ECF088-1AA8-4B9F-AF65-4E36EF10AF9C/Documents/myDatabase.sqlite Error: no such table: cards

Does anyone know if I'm doing something obviously wrong?

Thanks.

Aucun commentaire:

Enregistrer un commentaire