iOS-在创建孩子之前忽略了父母在父母上删除对象的核心数据

发布于 2025-02-08 14:24:06 字数 1064 浏览 1 评论 0原文

我不确定这是核心数据错误还是我在做错事。 我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文