How to copy sqlite database when application is launched in iOS?

前端 未结 6 1307
闹比i
闹比i 2020-12-01 13:20

I want to copy my sqlite database from the database location with latest updates to my iOS application every time I launch the application.

Is there any way to do i

6条回答
  •  温柔的废话
    2020-12-01 13:37

    Here it is in Swift 4/5

        func copyDatabaseIfNeeded(sourcePath : String) -> Bool {
          var destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
          destPath = destPath + "/foo.db3"
          let databaseExistsWhereNeeded = FileManager.default.fileExists(atPath: destPath)
          if (!databaseExistsWhereNeeded) {
            do {
                try FileManager.default.copyItem(atPath: sourcePath, toPath: destPath)
                print("db copied")
            }
            catch {
                print("error during file copy: \(error)")
            }
        }
        return true
    }
    

提交回复
热议问题