在核心数据中模拟数组类型属性的最佳方法是什么?
我有一个联系人列表,每个联系人都有几封电子邮件。
我是否应该创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
始终将核心数据视为对象图并相应地对数据进行建模。
您应该有一个联系人实体和一个电子邮件实体。电子邮件应位于与联系人建立一对多双向关系的另一端。如果您关心特定顺序,那么您还应该在电子邮件实体中具有一些可排序的值,以便稍后排序。
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.
这个解决方案听起来很合理。它仍然不是“数组类型属性”,因为多对多关系是无序集合而不是有序数组。
This solution sounds reasonable. Still it is not an "array type attribute" as to-many relations are unordered sets instead of ordered arrays.
您的实体图看起来像(伪代码):
在实体(抽象)和对象(具体)图中,您只需将联系人链接到他们的电子邮件,无需任何特定顺序。您不必担心实体图中关系的排序问题,因为您想要显示对象的顺序可能会随时发生变化。该顺序由每个特定获取请求的排序描述符确定。获取请求将以您定义的任何顺序返回一个数组。例如,一次您希望电子邮件按接收日期排序,另一次按发件人排序,另一次按其他属性排序。您甚至可以重新利用 fetch 返回的数组来准确获得您想要的顺序。
您只想确保实体具有捕获您要排序的信息的属性。
在实体图本身绝对需要某种类型的排序的非常罕见的情况中,您应该向实体本身添加排序属性并编写自定义代码来维护排序顺序。
Your entity graph would look something like (pseudocode):
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.