gpt4 book ai didi

ios - 如何在更新 iOS 应用程序时保存用户详细信息?

转载 作者:行者123 更新时间:2023-11-28 23:39:54 24 4
gpt4 key购买 nike

我有两个 sqlite 数据库,即 workout5.sqlite3 和 custom_ios.sqlite3。我在安装时将两个数据库复制到用户目录

if !fileManager.fileExists(atPath: dbPath) {
let fromPath: String? = Bundle.main.resourcePath?.stringByAppendingPathComponent(fileName as String)
var error : NSError?
do {
try fileManager.copyItem(atPath: fromPath!, toPath: dbPath)
}
catch let error1 as NSError {
error = error1
}

let alert: UIAlertView = UIAlertView()
if (error != nil) {
alert.title = "Error Occured"
alert.message = error?.localizedDescription
}
else {
alert.title = "Successfully Copy"
alert.message = "Your database copy successfully"
}
}

安装时,我检查文件是否存在。但是每次我更新应用程序时,数据库都会被替换。如何解决这个问题?

最佳答案

只有在卸载应用程序时才会删除文档目录。检查路径中是否存在可读文件:isReadableFile(atPath:)

示例代码如下:

let DBNAME = "myDB.sqlite"
let yourOriginalDatabasePath = URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent(DBNAME)
let pathsToDocuments = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let dbPath = URL(fileURLWithPath: pathsToDocuments[0]).appendingPathComponent(DBNAME)
print("dataBasebPath: \(dbPath.absoluteString)")

let isFileExist = FileManager.default.fileExists(atPath: dbPath.absoluteString)
let isReadableFileExist = FileManager.default.isReadableFile(atPath: dbPath.absoluteString)
if !isReadableFileExist && !isFileExist {
print("[DB] Copying DB to Documents Folder")
do {
try FileManager.default.copyItem(at: yourOriginalDatabasePath, to: dbPath)
} catch {
print("Fail to copy database from \(yourOriginalDatabasePath) to \(dbPath). Error: \(error)")
}
}

关于ios - 如何在更新 iOS 应用程序时保存用户详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53811486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com