Ios 删除核心数据数据库,并在更新时重新创建
当我对实现新核心数据实体的应用程序进行新升级时,我的应用程序将崩溃Ios 删除核心数据数据库,并在更新时重新创建,ios,swift,core-data,Ios,Swift,Core Data,当我对实现新核心数据实体的应用程序进行新升级时,我的应用程序将崩溃 如何删除整个核心数据数据库并重新创建它?我不在乎里面有什么,因为数据通常在应用加载时更新。它主要用于缓存,或者直到它可以与服务器同步为止。在初始化您的NSPersistentContainer之前: 删除您的Model.sqlitepersistant存储文件 删除同一目录中扩展名中包含.sqlite的所有其他文件(Model.sqlite shm,Model.sqlite wal) 这是我在实例化CoreDataContro
如何删除整个核心数据数据库并重新创建它?我不在乎里面有什么,因为数据通常在应用加载时更新。它主要用于缓存,或者直到它可以与服务器同步为止。在初始化您的
NSPersistentContainer
之前:
- 删除您的
persistant存储文件Model.sqlite
- 删除同一目录中扩展名中包含
的所有其他文件(.sqlite
,Model.sqlite shm
)Model.sqlite wal
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()
}
您不需要删除整个核心数据数据库。只需使用核心数据
迁移
就可以解决这个问题。这一个可以帮助@Poles迁移是一个容易出错的过程,很多简单的应用程序都希望删除表并重新开始。我实际上是在安卓系统中这样做的。我可以在我的AppDelegate中调用它吗?或者最好在哪里称呼它?我没有专用的CoreDataController。谢谢我暂时添加了一个迁移,但我认为我将来会需要它。所有与CoreData相关的内容都将进入。这比把所有的东西都放到应用程序中去要好,但是我不需要在应用程序完全加载之前调用它,否则我会得到一个关于不兼容数据库的错误吗?这就是为什么我想知道我是否可以在我的AppDelage中调用它。