为什么一个实体需要一个逆? [英] Why does an entity need an inverse?

查看:103
本文介绍了为什么一个实体需要一个逆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的核心数据模型,包含一个包含多个图像的单一图库。在我看来,它应该是足够的画廊有一个一对多的关系与图像,但如果我不建立反向关系从图像回到库我得到编译器警告和错误。从面向对象的角度来看,在我看来,一个图像不应该知道或关心它是在画廊或任何其他ManagedObject,为什么编译器强迫我创建这种反向关系?图片上的gallery属性看起来很错误。

I have a very simple Core data Model comprising of a single Gallery which contains multiple Images. It seems to me that it should be enough for a Gallery to have a one-to-many relationship with Image, but if I don't also establish an inverse relationship from Image back to gallery I get Compiler warnings and errors. From an object-oriented perspective it seems to me that an Image shouldn't know or care if it is in a Gallery or any other ManagedObject so why does the compiler force me to create this inverse relationship? Having a 'gallery' property on the image seems very wrong.

推荐答案

Core Data使用反向关系来帮助维护参照完整性。当您更改关系或删除对象时,会大大提高性能。没有逆,这些相同的动作可能导致全表扫描。

Core Data uses the inverse relationship to help maintain referential integrity. When you change a relationship or delete an object it speeds up performance dramatically. Without the inverse those same actions would potentially result in full table scans.

在我看来,没有一个反向应该是错误,而不是警告。

In my opinion, not having an inverse should be an error instead of a warning. The difference can be dramatic in some fairly common situations.

想象一下多实体级联删除没有它们。可能需要足够长的时间,以至于操作系统认为您的应用程序已崩溃。

Just imagine a multi-entity cascade delete without them. Could take long enough that the OS thinks your app has crashed.

这篇关于为什么一个实体需要一个逆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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