Ios 将persistentStoreCoordinator sqlite文件迁移到新名称
当我试图解决我的sqlite文件的问题时,我意识到在我的AppDelegate中有一个错误Ios 将persistentStoreCoordinator sqlite文件迁移到新名称,ios,swift,core-data,Ios,Swift,Core Data,当我试图解决我的sqlite文件的问题时,我意识到在我的AppDelegate中有一个错误 let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite") 我想更改文件名以匹配应用程序名,但我不想丢失SingleViewCoreData.sqlite文件中的当前数据。我怎样才能做到这一点 这就是我如何解决这个问题的方法,也是所有的Swift 3.1 var pURL
let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
我想更改文件名以匹配应用程序名,但我不想丢失
SingleViewCoreData.sqlite
文件中的当前数据。我怎样才能做到这一点 这就是我如何解决这个问题的方法,也是所有的Swift 3.1
var pURL: URL?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
checkForOldStore()
return true
}
然后我编辑了persistentStoreCoordinator部分
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
// let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
let url = self.pURL // ADDED THIS CODE WHICH REPLACED CODE FROM ABOVE.
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true])
} catch {
使用下面的代码,我首先用checkForOldStore()
检查旧sqlite文件是否存在。请注意,如果调用migrateStore()
之前文件存在,我将pURL设置为与persistentStoreCoordinator
上的旧设置相同。因为每次应用程序启动时,checkForOldStore()
仍然运行,而且现在文件不存在,所以我将pURL
设置为新文件名
func checkForOldStore() {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("SingleViewCoreData.sqlite")?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath!) {
print("FILE AVAILABLE")
pURL = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
migrateStore()
} else {
pURL = self.applicationDocumentsDirectory.appendingPathComponent("NewName.sqlite")
print("FILE NOT AVAILABLE")
}
}
在migrateStore()
的末尾,我为每个旧的sqlite文件调用remvoeOldDB
func migrateStore() {
// migrate current store from one URL to another
// write out the current store URL before the migration
var storeURL: URL? = persistentStoreCoordinator.persistentStores.last?.url
print("Current Store URL (before migration): \(String(describing: storeURL?.description))")
// grab the current store
let currentStore: NSPersistentStore? = persistentStoreCoordinator.persistentStores.last
// create a new URL
let newStoreURL: URL? = applicationDocumentsDirectory.appendingPathComponent("NewName.sqlite")
// setup new options dictionary if necessary
// migrate current store to new URL
_ = try? persistentStoreCoordinator.migratePersistentStore(currentStore!, to: newStoreURL!, options: nil, withType: NSSQLiteStoreType)
// and to check we're on the new store, write out tha URL again
storeURL = persistentStoreCoordinator.persistentStores.last?.url
print("Current Store URL (after migration): \(String(describing: storeURL?.description))")
removeOldDB(itemName: "SingleViewCoreData", fileExtension: "sqlite")
removeOldDB(itemName: "SingleViewCoreData", fileExtension: "sqlite-shm")
removeOldDB(itemName: "SingleViewCoreData", fileExtension: "sqlite-wal")
}
func removeOldDB(itemName:String, fileExtension: String) {
let fileManager = FileManager.default
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
guard let dirPath = paths.first else {
return
}
let filePath = "\(dirPath)/\(itemName).\(fileExtension)"
do {
try fileManager.removeItem(atPath: filePath)
} catch let error as NSError {
print(error.debugDescription)
}
}