简体   繁体   English

在核心数据中存储上下文属性的最佳方法?

[英]Best Way to Store Contextual Attributes in Core Data?

I am using Core Data to store objects. 我正在使用Core Data来存储对象。 What is the most efficient possibility for me (ie best execution efficiency, least code required, greatest simplicity and greatest compatibility with existing functions/libraries/frameworks) to store different attribute values for each object depending on the context, knowing that the contexts cannot be pre-defined, will be legion and constantly edited by the user? 对我来说最有效的可能性(即最佳执行效率,最少代码,最简单和与现有函数/库/框架的最大兼容性),根据上下文为每个对象存储不同的属性值,知道上下文不能是预定义,将由军团和不断编辑的用户?

Example: 例:

An Object is a Person (Potentially =Employer / =Employee) 对象是一个人(潜在=雇主/ =雇员)

Each person works for several other persons and has different titles in relation to their work relationships, and their title may change from one year to another (in case this detail matters: each person may also concomitantly employ one or several other persons, which is why a person is an employee but potentially also an employer) 每个人都为其他几个人工作,并且与他们的工作关系有不同的头衔,他们的头衔可能会从一年变为另一年(如果这个细节很重要:每个人也可能同时雇用一个或几个其他人,这就是为什么一个人是雇员,但也可能是雇主)

So one attribute of my object would be “Title vs Employer vs Year Ended” 所以我的对象的一个​​属性是“Title vs Employer vs Year Ended”

The best I could do with my current knowledge is save all three elements together as a string which would be an attribute value assigned to each object, and constantly parse that string to be able to use it, but this has the following (HUGE) disadvantages: 我能用现有知识做的最好的事情是将所有三个元素一起保存为一个字符串,该字符串将是分配给每个对象的属性值,并且不断解析该字符串以便能够使用它,但这具有以下(巨大的)缺点:

(1) Unduly Slowed Execution & Increased Energy Use. (1)执行速度过慢,能源使用增加。 Using this contextual attribute is at the very core of my prospective App´s core function (so it would literally be used 10-100 times every minute). 使用这个上下文属性是我预期的App核心功能的核心(所以它实际上每分钟使用10-100次)。 Having to constantly parse this information to be able to use it adds undue processing that I'd very much like to avoid (2) Undue Coding Overhead. 必须不断解析这些信息以便能够使用它增加了我不得不避免的过度处理(2)不正确的编码开销。 Saving this contextual attribute as a string will unduly make additional coding for me necessary each time I'll use this central information (ie very often). 将此上下文属性保存为字符串将在每次我使用此中心信息时(通常非常频繁地)为我提供额外的编码。 (3) Undue Complexity & Potential Incompatibility. (3)不合理的复杂性和潜在的不相容性。 It will also add undue complexity and by departing from the expected practice it will escape the advantages of Core Data. 它还会增加不必要的复杂性,并且脱离预期的做法,它将逃脱Core Data的优势。

What would be the most efficient way to achieve my intended purpose without the aforementioned disadvantages? 没有上述缺点,实现我的预期目的的最有效方法是什么?

Taking your example, one option is to create an Employment entity, with attributes for the title and yearEnded and two (to-one) relationships to Person . 以你的例子,一个选择是创建一个Employment实体,与属性titleyearEnded和两个(一对一)关系到Person One relationship represents the employer and the other represents the employee . 一种关系代表employer ,另一种代表employee

The inverse relationships are in both cases to-many. 两种情况下的反比关系都很多。 One represents the employments where the Person is the employee (so you might name it employmentsTaken ) and the other relationship represents the employments where the Person is the Employer (so you might name it employmentsGiven ). 一个代表雇员是雇员的雇员(因此你可以将雇员命名为employmentsTaken ),而另一个代表就是雇员是雇主的雇佣关系(所以你可以将其命名为employmentsGiven )。

Generalising, this is the solution recommended by Apple for many-many relationships which have attributes (see "Modelling a relationship based on its semantics" in their documentation ). 概括,这是Apple推荐的许多具有属性的关系的解决方案(参见文档中的 “基于其语义建立关系”)。

Whether that will address all of the concerns listed in your question, I leave to your experimentation: if things are changing 10-100 times a minute, the overhead of fetch requests and creating/updating/deleting the intermediate ( Employment ) entity might be worse than your string representation. 这是否会解决您问题中列出的所有问题,我将继续您的实验:如果事情每分钟改变10-100次,则获取请求和创建/更新/删除中间( Employment )实体的开销可能会更糟比你的字符串表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM