我相当确定 CoreData 会抛出标题错误,因为函数 addNewGoal
中的行 let newGoal = Goal(context: viewContext)
。但我不知道另一种方法来“重新表述”我的函数。
相同错误的 stackoverflow 搜索结果似乎与我这里的问题无关。 (一个是通过使用静态变量而不是惰性变量解决的;另一个是关于这个错误随机发生的)
提前谢谢
(如果这是一个愚蠢的问题,我深表歉意。我是一个通过构建项目来学习的初学者。CoreData 对于我的项目概念至关重要,但我完全超出了我的能力范围,它让我内心崩溃。)
import CoreData
struct PersistenceController {
static let shared = PersistenceController()
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let newItem = Goal(context: viewContext)
}
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
let container: NSPersistentContainer
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "Project")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
func addNewGoal() {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
let newGoal = Goal(context: viewContext)
newGoal.passed = true
newGoal.title = "New Goal \(Date())"
newGoal.date = Date()
}
func addNewFail() {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
let newGoal = Goal(context: viewContext)
newGoal.passed = false
newGoal.title = "New Goal \(Date())"
newGoal.date = Date()
}
}
错误
2021-08-11 05:05:44.981041+0800 Project[27243:1370905] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Goal' so +entity is unable to disambiguate.
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Goal' so +entity is unable to disambiguate.
2021-08-11 05:05:44.981226+0800 Project[27243:1370905] [error] warning: 'Goal' (0x6000032cc370) from NSManagedObjectModel (0x6000026fa7b0) claims 'Goal'.
CoreData: warning: 'Goal' (0x6000032cc370) from NSManagedObjectModel (0x6000026fa7b0) claims 'Goal'.
2021-08-11 05:05:44.981343+0800 Project[27243:1370905] [error] warning: 'Goal' (0x6000032d5e40) from NSManagedObjectModel (0x6000026bc5f0) claims 'Goal'.
CoreData: warning: 'Goal' (0x6000032d5e40) from NSManagedObjectModel (0x6000026bc5f0) claims 'Goal'.
2021-08-11 05:05:44.981447+0800 Project[27243:1370905] [error] error: +[Goal entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[Goal entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
最佳答案
首先,摆脱:
let result = PersistenceController(inMemory: true)
行并创建下一行:
let viewContext = container.viewContext
您已经将 container
变量范围限定为整个结构,因此它可以在 PersistenceController
结构中使用。
你的函数是这样的:
func addNewGoal() {
let viewContext = container.viewContext
let newGoal = Goal(context: viewContext)
newGoal.passed = true
newGoal.title = "New Goal \(Date())"
newGoal.date = Date()
}
关于错误,根据this answer问题是由于同时加载两个模型引起的,并且通过调用 PersistenceController(inMemory: true)
我认为您可能已经做到了这一点。
此外,拥有 shared
var 的全部意义在于,您只使用 PersistenceController
的一个实例。您当然不想通过传递已初始化的 inMemory: true
来初始化它。
关于swift - 核心数据 : Failed to find a unique match for an NSEntityDescription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68734033/