核心数据:两个相同类型关系的逆关系 [英] Core Data: inverse relationship for two relationships with same type

查看:18
本文介绍了核心数据:两个相同类型关系的逆关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序核心数据模型中,我有 Sheet 和 Text 实体.工作表实体可以有两个文本:privacyNotes 和 termsOfUse.

In my app Core Data model I have Sheet and Text entities. Sheet entity can have two Text's: privacyNotes and termsOfUse.

两种文本类型.因此,在 XCode 数据建模器中,我在带有文本目标的 Sheet 中创建了称为privacyNotes"和termsOfUse"的一对一关系.接下来是文本中的一对一关系表".然后我选择该 Text.sheet 关系作为 Sheet.privacyNotes 的反向关系.到现在为止还挺好.但是当我将相同的 Text.sheet 关系设置为反向 Sheet.termOfUse XCode 删除此关系作为反向 Sheet.privacyNotes!

Both of Text type. So in XCode data modeler I create to-one relationships called "privacyNotes" and "termsOfUse" in Sheet with Text destination. Next goes to-one relationship "sheet" in Text. Then I select that Text.sheet relationship as inverse for Sheet.privacyNotes. So far so good. But when I set same Text.sheet relationship as inverse for Sheet.termOfUse XCode deletes this relationship as inverse Sheet.privacyNotes!

我知道与 Objective-C 对象关系相比,DB 中的关系可能不是那么简单,但我真的不明白为什么 SQLite 或 (CoreData) 不能重用一种关系作为对少数其他关系的反向关系?

I understand that relationships in DB can be not so simple compared to Objective-C objects relationships, but I really don't get why SQLite or (CoreData) can't reuse one relationship as inverse for FEW other relationships?

推荐答案

稍微了解一下抽象引擎可能会有所启发*:一个关系只能是另一个关系的逆,因为在后备存储中,它们重新用相同的数据表示.如果 Text 和 Sheet 可以有某种关系,Core Data 会做一个优秀的人类数据建模者会做的事情,并尽可能简洁地存储这种关系.实体对象的关系属性只是查看这种关系的方式.

A little peek under the abstraction hood might be enlightening*: a relation can only be the inverse for exactly one other relation because, in the backing store, they're represented by the same data. If a Text and a Sheet can have a certain relationship, Core Data does what a good human data modeler would do and stores that relationship as succinctly as possible. The relation properties of the entity objects are just ways of looking at that relationship.

要获得您想要的效果:继续为privacyNote 和termsOfUse 提供Sheet 属性;但是给像 sheetIAmTermsFor 和 sheetIAmPrivacyNoteFor 这样的文本属性,并适当地将它们设置为逆.然后在 Text 类中,添加一个合成属性,如下所示:

To get the effect of what you're going for: go ahead and give Sheet properties for privacyNote and termsOfUse; but give Text properties like sheetIAmTermsFor and sheetIAmPrivacyNoteFor, and set them as inverses appropriately. Then in the Text class, add a synthetic property along these lines:

// in interface
@property (nonatomic, readonly) Sheet *sheet;
// in impl
-(Sheet *)sheet
{
  if ([self sheetIAmTermsFor])
    return [self sheetIAmTermsFor];
  else
    return [self sheetIAmPrivacyNoteFor];
}

如果你也想写一个 setter,你必须决定这个 setter 应该赋予 Text 哪个角色(Core Data 无法为你弄清楚,另一个原因是属性不能是相反的两个不同的属性.)

If you want to write a setter too, you'll have to decide which role that setter should bestow on the Text (which Core Data can't figure out for you, another reason a property can't be the inverse of two different properties.)

如果您需要强制限制文本只能是privacyNote"或terms",但不能同时是两者,请按照 Apple 在docs,并在设置时将其他属性设为 null.

If you need to enforce a constraint that a Text can only ever be a "privacyNote" or a "terms" but never both, override the setters for sheetIAmTermsFor and sheetIAmPrivacyNoteFor, following Apple's pattern in the docs, and have each null the other property when set.

(* Apple 认为 Core Data 生成的 SQLite 数据库对其实现来说是私有的,但检查它们的模式可能很有教育意义.只是不要试图编写在 CD 后面直接戳数据库的运输代码.)

(* Apple regards the SQLite databases Core Data generates as private to their implementation, but inspecting their schemas can be very educational. Just don't be tempted to write shipping code that goes behind CD's back to poke at the db directly.)

这篇关于核心数据:两个相同类型关系的逆关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆