简体   繁体   English

核心数据一对一单向关系

[英]Core data one-to-one unidirectional relationship

I have 2 entities in which totally 12 properties are there of 3 variations of min, max and average of some particular type of fields. 我有2个实体,其中总共12个属性存在某些特定类型字段的最小值,最大值和平均值的3种变化。 Hence I refactored the 2 entities into 3 entities making the 3rd entity as 'Values' which contains 3 properties ie min, max and average. 因此,我将2个实体重构为3个实体,使第3个实体成为“值”,其中包含3个属性,即最小值,最大值和平均值。 And reduced the 12 properties into 4 relationships. 并将12个属性简化为4个关系。 Here's a replica of my models as an example in the image below. 这是我的模型的副本,如下图所示。

在此处输入图片说明

As you can see I have one-to-one unidirectional relationship with the 3rd entity. 如您所见,我与第三个实体具有一对一的单向关系。 However Xcode keeps on complaining about 'Inverse' relationship. 但是Xcode一直抱怨“逆”关系。

在此处输入图片说明

As per me I can add 4 relationships in 'Values' and update all to become 'inverse', however this doesn't seem to be the right solution as when the second entity gets tied to the 'Values', it will have additional 3 nil relationships and whereas in case of first it will have 1 additional nil relationship. 按照我的说法,我可以在“值”中添加4个关系并将所有关系更新为“逆”,但这似乎不是正确的解决方案,因为当第二个实体与“值”绑定时,它将有另外3个关系零关系,而在第一种情况下,它将有1个其他零关系。 Both of these are unnecessary. 这两个都是不必要的。

Refactoring 'Values' and splitting it into two similar entities also is not a good solution either I believe. 我认为,重构“价值”并将其分成两个相似的实体也不是一个好的解决方案。

Hence can anyone suggest me what is the right approach or best practice to solve this problem. 因此,有人可以建议我什么是解决此问题的正确方法或最佳实践。 Let me know if I'm unclear anywhere while describing my issue. 让我知道在描述我的问题时是否不清楚。

Based on your description, I would undo the refactoring and go back to using properties instead of relationships. 根据您的描述,我将撤消重构并返回使用属性而不是关系。 You're adding complexity for no real benefit, and the Values entity is (as you're finding) too generic to really be useful or meaningful. 您正在添加复杂性而没有任何实际好处,而Values实体(正如您所发现的)过于笼统而无法真正有用或有意义。 This refactoring isn't serving any useful purpose; 这种重构没有任何用处。 don't fix it, revert it. 不要修复它,还原它。

You should look into Weak Relationships (Fetched Properties) for how to manage relationships correctly and the solution for your error code. 您应该研究弱关系(获取的属性),以了解如何正确管理关系以及错误代码的解决方案。

Most object relationships are inherently bidirectional. 大多数对象关系本质上是双向的。 If a Department has a to-many relationship to the Employees who work in a Department, there is an inverse relationship from an Employee to the Department that is to-one. 如果部门与在部门中工作的雇员有多对多关系,则从雇员到部门之间存在一对一的逆关系。 The major exception is a fetched property, which represents a weak one-way relationship—there is no relationship from the destination to the source. 主要的例外是获取的属性,它表示弱的单向关系-从目标到源没有关系。

Also, if you want to make things easier, you should look into (if possible) avoid 3 objects and have a single object, or two objects, with the propertiesToFetch of the NSFetchRequest in mind. 另外,如果您想使事情变得简单,则应考虑(如果可能)避免使用3个对象,而要考虑到NSFetchRequestpropertiesToFetch ,使一个或两个对象成为NSFetchRequest This way you can fetch your Entity , keep the properties in a single Entity , but only fetch the properties you want and avoid the overhead and memory consumption of fetching properties you are not going to use. 这样,您可以获取Entity ,将属性保留在单个Entity但仅获取所需的属性,并避免获取不需要的属性时的开销和内存消耗。

Whichever fits your needs, you have the options. 无论您选择哪种方式,都可以选择。 GL GL

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

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