重新創建它,當我推出了全新的升級,以我的應用程序實現新的核心數據實體,我的應用程序會崩潰。刪除核心數據的數據庫,並更新
如何刪除整個核心數據的數據庫,並重新創建它?我不照顧什麼在那裏,因爲數據是在應用程序加載時,經常更新的反正。它主要用於高速緩存,或者直到它可以與服務器同步。
重新創建它,當我推出了全新的升級,以我的應用程序實現新的核心數據實體,我的應用程序會崩潰。刪除核心數據的數據庫,並更新
如何刪除整個核心數據的數據庫,並重新創建它?我不照顧什麼在那裏,因爲數據是在應用程序加載時,經常更新的反正。它主要用於高速緩存,或者直到它可以與服務器同步。
初始化之前您NSPersistentContainer
:
Model.sqlite
持久性存儲文件.sqlite
的擴展名(Model.sqlite-shm
,Model.sqlite-wal
)。這是什麼,當我實例化我CoreDataController我使用:
private override init() {
// This resource is the same name as your xcdatamodeld contained in your project.
guard let modelURL = NSBundle(forClass:CoreDataController.classForCoder()).URLForResource("YOURAPP", withExtension:"momd") else {
fatalError("Error loading model from bundle")
}
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
guard let mom = NSManagedObjectModel(contentsOfURL: modelURL) else {
fatalError("Error initializing mom from: \(modelURL)")
}
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
self.managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
self.managedObjectContext.persistentStoreCoordinator = psc
let storeURL = CoreDataController.storeUrl
do {
try psc.addPersistentStoreWithType(CoreDataController.storeType(), configuration: nil, URL: storeURL, options: nil)
} catch { //Exception means your database leads to error, so drop it, and create a new one
do {
try NSFileManager.defaultManager().removeItemAtURL(storeURL)
try psc.addPersistentStoreWithType(CoreDataController.storeType(), configuration: nil, URL: storeURL, options: nil)}
catch {
fatalError("Error migrating store: \(error)")
}
}
super.init()
}
您不必刪除整個核心數據的數據庫。只需使用核心數據'Migration'來解決這個問題。 – Poles
這可以幫助http://jamesonquave.com/blog/core-data-migrations-swift-tutorial/ – Poles
@Poles遷移是一個容易出錯的過程,很多應用程序簡單地想要刪除表並重新開始。例如,我在Android中就是這麼做的。 –