0

I used swiftData to store information on sqlite database till today it will work fine and suddenly it created a new location of database as well as simulator location when first time run it shows

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/2DB733AF-2544-4256-B1E5-5E8725E51CDF/Documents/DataBase.db

second time

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/551991FA-392A-40E9-810E-31CEFCD3069A/Documents/dataBase.db

third time

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/03E4BE03-D6E7-47BF-A98D-A129DF09DD28/Documents/DataBase.db

i used this code

  let fileManager = NSFileManager()
        var Sourcepath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("PhotoKeeper.db");
        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        let databaseStr = "DataBase.db"
        let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
        println(dbPath)    
        if(fileManager .fileExistsAtPath(dbPath) == false) {


            var error:NSError?
            fileManager.copyItemAtPath(Sourcepath!, toPath: dbPath, error: &error)
            println(error)

        }

my problem is every time new instance of database is created i.e. if i insert 2 row and compile and run the app it will create new database with zero row

Rizwan Shaikh
  • 2,824
  • 2
  • 27
  • 49

1 Answers1

0

You should save only relative path of your contents (document, db, ...) cause XCode with iOS8 (I suppose) changes application folder whenever you build and run it.

For example, you have your .db file in Documents, so just save your link as "DataBase.db". Then, when you need to access to this file, get the path as:

let documentsFolder = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
let dbPath = documentsFolder.stringByAppendingPathComponent("Database.db")
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • hello , i used the same code now my problem is every time new instance of database is created i.e. if i insert 2 row and compile and run the app it will create new database with zero row – Rizwan Shaikh Apr 06 '15 at 05:50
  • sorry, your given code is wright . I face the problem in my insert logic – Rizwan Shaikh Apr 06 '15 at 07:31