在核心数据中模拟数组类型属性的最佳方法是什么?

发布于 2024-09-12 20:31:56 字数 201 浏览 7 评论 0原文

我有一个联系人列表,每个联系人都有几封电子邮件。

我是否应该创建一个 Contact 核心数据实体和一个 Email 实体,并将多个电子邮件对象链接到一个联系人对象?或者我应该采取另一种方式,例如连接所有电子邮件并将它们存储为一个大字符串?

处理这种配置最干净、最有效的方法是什么?

谢谢

I've got a list of contacts, each having several emails.

Should I create a Contact Core Data entity and a Email entity and link several email objects to one contact object? Or should I do it another way e.g concatenate all the emails and store them as one big string?

What's the cleanest and most efficient way to deal with such a configuration ?

Thanks

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

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

发布评论

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

评论(3

々眼睛长脚气 2024-09-19 20:31:56

始终将核心数据视为对象图并相应地对数据进行建模。

您应该有一个联系人实体和一个电子邮件实体。电子邮件应位于与联系人建立一对多双向关系的另一端。如果您关心特定顺序,那么您还应该在电子邮件实体中具有一些可排序的值,以便稍后排序。

Always think of Core Data as an object graph and model your data accordingly.

You should have a Contact entity and an Email entity. The email should be on the other end of a one-to-many bi-directional relationship with Contact. If you care about a specific order then you should also have some orderable value in the Email entity for later sorting.

唔猫 2024-09-19 20:31:56

我是否应该创建一个联系人 CoreData 实体和一个电子邮件实体,并将多个电子邮件对象链接到一个联系人对象?

这个解决方案听起来很合理。它仍然不是“数组类型属性”,因为多对多关系是无序集合而不是有序数组。

Should I create a contact CoreData entity and a email entity and link several email objects to one contact object ?

This solution sounds reasonable. Still it is not an "array type attribute" as to-many relations are unordered sets instead of ordered arrays.

百思不得你姐 2024-09-19 20:31:56

您的实体图看起来像(伪代码):

Contact{
    name:string
    emailAddress:string
    //...other attributes of contacts
    emails<--(optional,cascade)-->>Email.contact
}

Email{
    from:string
    // ... other attributes of emails
    contact<<--(required,nullify)-->Contact.emails
}

在实体(抽象)和对象(具体)图中,您只需将联系人链接到他们的电子邮件,无需任何特定顺序。您不必担心实体图中关系的排序问题,因为您想要显示对象的顺序可能会随时发生变化。该顺序由每个特定获取请求的排序描述符确定。获取请求将以您定义的任何顺序返回一个数组。例如,一次您希望电子邮件按接收日期排序,另一次按发件人排序,另一次按其他属性排序。您甚至可以重新利用 fetch 返回的数组来准确获得您想要的顺序。

您只想确保实体具有捕获您要排序的信息的属性。

在实体图本身绝对需要某种类型的排序的非常罕见的情况中,您应该向实体本身添加排序属性并编写自定义代码来维护排序顺序。

Your entity graph would look something like (pseudocode):

Contact{
    name:string
    emailAddress:string
    //...other attributes of contacts
    emails<--(optional,cascade)-->>Email.contact
}

Email{
    from:string
    // ... other attributes of emails
    contact<<--(required,nullify)-->Contact.emails
}

In both the entity (abstract) and object (concrete) graphs, you need only link contacts to their emails without any particular order. You shouldn't worry about ordering the relationships in the entity graph because the order in which you want to display the objects might change from moment-to-moment. That order is determined by the sort descriptor of each particular fetch request. The fetch request will return an array in any order you define. E.g one time you want emails sorted by date received, another time by from, another time by some other attribute. You can even resort the array the returned by a fetch to get exactly the order you want.

You just want to make sure that the entities have attributes that capture the information on which you want to sort.

In the very rare cases in which some type of ordering is absolutely required in the entity graph itself, you should add a ordering attribute to the entity itself and write custom code to maintain the sort order.

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