Objective C - 核心数据关系

发布于 2024-11-04 07:13:37 字数 189 浏览 4 评论 0原文

我的核心数据关系有问题。 我向方法 addData 传递了一个数据数组。它是一个字典数组。每个字典都有 3 个键,“A”、“B”、“C”。键 C 存储另一个具有 3 个键的字典数组。 第一个数组是项目类型,第二个数组是项目子类型。 现在,我如何将它放入 CoreData 中?实体名称是“Type”和“Subtype”。 我如何从 CoreData 获取它? 谢谢

i've a problem with Core Data relationship.
I pass to my method addData an array of data. It is an array of dictionary. Every dictionary have 3 keys, "A", "B", "C". Key C store another array of dictionary that have 3 keys.
The first array is of item Type and the second of item Subtype.
Now, how can i place it into CoreData?The entity name is "Type" and "Subtype".
And how can i take it from CoreData?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-11-11 07:13:37

您需要一个 Type 实体,该实体将具有属性 ab 以及关系 c(小写以符合与命名约定)。 cSubtype 实体是一对多关系。 Subtype 实体具有三个属性,一个属性对应字典的每个键,还有一个关系 type,该关系是 Type 对象的 c 的逆关系。

使用以下伪代码(我不关心设置核心数据的细节。您可以轻松阅读 docs) 帮助您创建一个解决方案来填充原始模型字典数组。

for (NSDictionary* aType in typeArray)
{
    // Create a managed object for the dictionary called aTypeManagedObject

    [aTypeManagedObject setValue: [aType objectForKey: @"A"] forKey: @"a"];
    [aTypeManagedObject setValue: [aType objectForKey: @"B"] forKey: @"b"];

    for (NSDictionary* subType in [aType objectForKey: @"C"])
    {
        // Create a managed object for the dictionary called aSubtypeManagedObject

        // set the attributes in the same way as a and b above

        [aSubTypeManagedObject setValue: aType forKey: @"type"]; // Automatically updates the aType's c relationship
    }
}

// Save all the changes

下次您从核心数据存储中获取类型管理对象时,所有这些都将已设置完毕。

[aTypeManagedObject valueForKey: @"c"]

将返回该 Type 对象的子类型对象的数组。

You need an entity for Type which will have attributes a and b and a relationship c (lower case to conform with naming conventions). c is a 1 to many relationship to the Subtype entity. The Subtype entity has three attributes, one for each of the dictionary's keys and a relationship type which is the inverse relationship for the Type object's c.

Use the following pseudocode (I can't be bothered with the detail of setting up core data. You can easily read the docs) to help you create a solution to populate your model from the original array of dictionaries.

for (NSDictionary* aType in typeArray)
{
    // Create a managed object for the dictionary called aTypeManagedObject

    [aTypeManagedObject setValue: [aType objectForKey: @"A"] forKey: @"a"];
    [aTypeManagedObject setValue: [aType objectForKey: @"B"] forKey: @"b"];

    for (NSDictionary* subType in [aType objectForKey: @"C"])
    {
        // Create a managed object for the dictionary called aSubtypeManagedObject

        // set the attributes in the same way as a and b above

        [aSubTypeManagedObject setValue: aType forKey: @"type"]; // Automatically updates the aType's c relationship
    }
}

// Save all the changes

Next time you fetch the Type managed object from the core data store, all that will be set up already.

[aTypeManagedObject valueForKey: @"c"]

will return an array of the subtype objects for that Type object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文