我试图弄清楚我需要什么合并策略。为此,我执行了以下操作:
之后,我编写了简单的测试:
let name1 = "User1"
let name2 = "User2"
self.createUser(identifier: 0, name: name1)
do {
try moc.save()
let request: NSFetchRequest<User> = User.fetchRequest()
let result = try moc.fetch(request)
expect(result.first?.preferredName) == name1
} catch {
fail("\(error) \(error.localizedDescription)")
}
self.createUser(identifier: 0, name: name2)
do {
try moc.save()
let request: NSFetchRequest<User> = User.fetchRequest()
let result = try moc.fetch(request)
//it is strange that name is not overwrites
expect(result.first?.preferredName) == name1
} catch {
fail("\(error) \(error.localizedDescription)")
}
第二次读取后,我不知道应该期待什么。 (默认合并策略会导致错误,应该如此)