私は問題を解決することができたかこれも、これはすべてのスウィフト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 {
古いsqliteのファイルがcheckForOldStore()
で存在する場合、私は最初に確認してください。なお、migrateStore()
を呼び出す前にファイルが存在する場合は、pURLをpersistentStoreCoordinator
の古い設定と同じに設定します。そして、checkForOldStore()
はアプリが起動するたびに実行され、ファイルが存在しないので、私はpURL
を新しいファイル名に設定します。私は昔のsqliteファイルごとにremvoeOldDB
呼び出すmigrateStore()
の終わりに
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")
}
}
。
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)
}
}