iOS-在创建孩子之前忽略了父母在父母上删除对象的核心数据
我不确定这是核心数据错误还是我在做错事。 我在ViewContext上有一个对象。我在ViewContext上删除对象,然后以ViewContext作为父级创建子上下文。父表示“ someObject.isdeleted”为true(正确)。
父母和孩子尚未保存。
子上下文表明该对象仍然存在,iSdeleted
是错误的。 (不正确)。
我仅更新对象的属性并添加新对象进行了测试,并且它可以正常工作,但是删除却没有。
我在这里做错了什么?
我知道,如果变化在创建孩子后发生(假设没有保存),则父母的更改不会自动将其推向孩子,但是当孩子第一次创建时,孩子不应该与父母处于同一状态吗?
func testDelete(someObject: NSManagedObject) {
guard let parentContext = someObject.managedObjectContext else { return }
let objectID = someObject.objectID
parentContext.performAndWait {
parentContext.delete(someObject)
print("Parent isDeleted: \(someObject.isDeleted)")
// Results: "Parent isDeleted: true"
}
let childContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
childContext.parent = parentContext
childContext.performAndWait {
let childObject = childContext.object(with: objectID)
print("Child isDeleted: \(childObject.isDeleted)")
// Results: "Parent isDeleted: false"
}
}
I'm not sure if this is a Core Data bug or I'm doing something wrong.
I have an object on viewContext. I delete the object on viewContext, then create a child context with viewContext as the parent. Parent indicates that 'someObject.isDeleted` is true (correctly).
Neither the parent nor child are saved yet.
The child context indicates that the object still exists and isDeleted
is false. (incorrect).
I tested with just updating the object's properties and adding new objects and it works properly, but deleting does not.
What am I doing wrong here?
I understand that changes on a parent are not automatically pushed to child IF the changes occur AFTER creating the child (assuming nothing saved), but shouldn't the child be in the same state as parent when first created?
func testDelete(someObject: NSManagedObject) {
guard let parentContext = someObject.managedObjectContext else { return }
let objectID = someObject.objectID
parentContext.performAndWait {
parentContext.delete(someObject)
print("Parent isDeleted: \(someObject.isDeleted)")
// Results: "Parent isDeleted: true"
}
let childContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
childContext.parent = parentContext
childContext.performAndWait {
let childObject = childContext.object(with: objectID)
print("Child isDeleted: \(childObject.isDeleted)")
// Results: "Parent isDeleted: false"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论