[英]CoreData Lightweight migration issue
I have written this app in Swift 5 and is already LIVE in App Store.我用 Swift 5 编写了这个应用程序,并且已经在 App Store 上线了。
Now, I just want to add a new single boolean attribute to one existing entity in coredata.现在,我只想向 coredata 中的一个现有实体添加一个新的单一布尔属性。
For this purpose, I followed steps:为此,我遵循了以下步骤:
Issue:问题:
While in app, I am successfully able to save data in coredata and retrieve from coredata in any ViewController but, if app opens from start, it cannot find previous file and recreates coredata file.在应用程序中,我能够成功地将数据保存在 coredata 中并在任何 ViewController 中从 coredata 检索,但是,如果应用程序从头开始打开,它找不到以前的文件并重新创建 coredata 文件。
Whenever I open app, it shows error in xCode console:每当我打开应用程序时,它都会在 xCode 控制台中显示错误:
Connecting to sqlite database file at "/dev/null"
AppDelegate code: AppDelegate代码:
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Project")
let description = NSPersistentStoreDescription()
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions=[description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container }()
The error message could indicate that the app doesn't know where to find or save the database files.错误消息可能表明应用程序不知道在哪里可以找到或保存数据库文件。 The phrase "/dev/null"
indicates in memory storage.短语"/dev/null"
表示在 memory 存储中。
Try to use尝试使用
let description = NSPersistentStoreDescription(url: <- File url to the main database file ->)
instead of the the current instantiation.而不是当前的实例化。
You need to specify the URL for the location of the actual model's database location.您需要指定 URL 为实际模型的数据库位置。
guard let storeURL = try? FileManager
.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("yourModel.sqlite") else {
fatalError("Error finding Model from File Manager")
}
let container = NSPersistentContainer(name: yourModelName, managedObjectModel: yourModelInstance)
let description = NSPersistentStoreDescription(url: storeURL)
description.type = NSSQLiteStoreType
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [description]
container.loadPersistentStores(completionHandler: { storeDescription, error in
After changing container.persistentStoreDescriptions = description
to container.persistentStoreDescriptions.append(description)
it has been fixed.将container.persistentStoreDescriptions = description
更改为container.persistentStoreDescriptions.append(description)
后,它已得到修复。 Full code:完整代码:
public lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: dataModelName)
var description = NSPersistentStoreDescription()
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions.append(description)
container.viewContext.mergePolicy = NSMergePolicy.overwrite
container.loadPersistentStores { _, error in
if let error = error {
}
}
return container
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.