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

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

问题描述

您好
在我的应用程序的核心数据模型我有工作表和文本实体。表单实体可以有两个文本:privacyNotes和termsOfUse。两种文本类型。因此,在XCode数据建模器中,我在Sheet with Text目标中创建一个名为privacyNotes和termsOfUse的一对一关系。接下来在文本中转到一个关系工作表。然后,我选择Text.sheet关系作为Sheet.privacyNotes的反。到现在为止还挺好。但是当我将相同的Text.sheet关系设置为与Sheet.termOfUse相反XCode删除此关系为反过来Sheet.privacyNotes!
我理解DB中的关系可能不像Objective-C对象关系那么简单,但我真的不知道为什么SQLite或(CoreData)不能重复使用一个关系作为FEW其他关系的逆? / p>

Hi In my app Core Data model I have Sheet and Text entities. Sheet entity can have two Text's: privacyNotes and termsOfUse. 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! 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?

推荐答案

抽象罩下的一点点可能是启发*:关系只能是一个其他关系的逆,在后备存储中,它们由相同的数据表示。如果文本和工作表可以具有特定关系,则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.

为了获得你想要的效果:继续给Sheet属性隐私声明和条款但是给予text属性如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,设置者应该在文本上赋予的角色(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,但不能同时包含两者,请覆盖sheetIAmTermsFor和sheetIAmPrivacyNoteFor的设置,遵循Apple在文档中的模式,

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认为SQLite数据库核心数据生成为私有的实现,但检查他们的模式可以是非常有教育意义的。只是不要试图编写运输代码,后面的CD背回到戳直接db)。

(* 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天全站免登陆