每个核心数据关系都必须有一个反向关系吗? [英] Does every Core Data Relationship have to have an Inverse?

查看:24
本文介绍了每个核心数据关系都必须有一个反向关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个实体类:SocialAppSocialAppType

Let's say I have two Entity classes: SocialApp and SocialAppType

SocialApp 中,我有一个属性:appURL 和一个关系:type.

In SocialApp I have one Attribute: appURL and one Relationship: type.

SocialAppType 中,我有三个属性:baseURLnamefavicon.

In SocialAppType I have three Attributes: baseURL, name and favicon.

SocialApp 关系type 的目标是SocialAppType 中的单个记录.

The destination of the SocialApp relationship type is a single record in SocialAppType.

例如,对于多个 Flickr 帐户,将有多个 SocialApp 记录,每个记录都包含一个指向个人帐户的链接.Flickr"将有一个 SocialAppType 记录.类型,所有 SocialApp 记录将指向.

As an example, for multiple Flickr accounts, there would be a number of SocialApp records, with each record holding a link to a person's account. There would be one SocialAppType record for the "Flickr" type, that all SocialApp records would point to.

当我使用此架构构建应用程序时,我收到一条警告,指出 SocialAppTypeSocialApp 之间没有反向关系.

When I build an application with this schema, I get a warning that there is no inverse relationship between SocialAppType and SocialApp.

 /Users/username/Developer/objc/TestApp/TestApp.xcdatamodel:SocialApp.type: warning: SocialApp.type -- relationship does not have an inverse

我是否需要逆,为什么?

Do I need an inverse, and why?

推荐答案

在实践中,我没有因为没有逆而丢失任何数据 - 至少我知道.快速 Google 建议您应该使用它们:

In practice, I haven't had any data loss due to not having an inverse - at least that I am aware of. A quick Google suggests you should use them:

反向关系不仅仅让事情更整洁,实际上是Core Data 使用它来维护数据诚信.

An inverse relationship doesn't just make things more tidy, it's actually used by Core Data to maintain data integrity.

-- 可可开发中心

您通常应该建模双向的关系,以及指定逆关系适当地.核心数据使用这个信息以确保一致性如果更改是对象图的制作(参见操纵关系和对象图完整性").为一个一些原因的讨论你可能不想建模两个方向的关系,以及可能出现的一些问题如果没有,请参阅单向关系."

You should typically model relationships in both directions, and specify the inverse relationships appropriately. Core Data uses this information to ensure the consistency of the object graph if a change is made (see "Manipulating Relationships and Object Graph Integrity"). For a discussion of some of the reasons why you might want to not model a relationship in both directions, and some of the problems that might arise if you don’t, see "Unidirectional Relationships."

-- 核心数据编程指南

这篇关于每个核心数据关系都必须有一个反向关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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