核心数据继承vs无继承 [英] Core Data inheritance vs no inheritance

查看:110
本文介绍了核心数据继承vs无继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有核心数据的一些问题,所以我希望有人能够帮助我)。

i'm having some issues with core data, so i hope that someone will be able to help me :)

第一个问题,我有一个数据模型看起来像这样:

First problem, i have a data model that looks like this :

 Entity P (A) <----> Entity R
  / | \
 / /\ \ 
/ / | \ \
C D E F G

我的所有实体都继承自同一实体P,因为他们需要一个共同的属性和共同的关系(A和R)

All my entities inherit from the same entity "P" because they need a common attribute and a common relationship ("A" and "R")

我得到的问题是核心数据使用只生成一个sqlite表为所有实体当你使用继承。在我的情况下,这意味着我的数据库将只有一个表的所有数据。我做了一些研究,我看到它创造性能问题(而且所有我的实体属性是瞬态和在willSave他们的值被聚合+加密到一个NSData存储在A,所以我将无法使用谓词过滤并提高SELECT性能)。所以我决定删除P,并将A添加到C,D...,G。问题是R,因为之前我只有一个反向关系,现在我需要创建一个每次我创建一种新的实体。
所以我想删除所有的逆关系,是可能吗?
有时我需要创建一个具有nil上下文的托管对象,并且我以后将它们插入上下文,这可能是为什么反向关系不是由核心数据自动设置,如果我在插入之前设置非逆MOC是吗?

The problem i am getting is that core data uses generates only one sqlite table for all the entities when you use inheritance. In my case it means that my database will have only 1 table for all the datas. I did some research and i saw that it creates performance issues (moreover all my entities attributes are transients and during willSave their values are aggregated+encrypted into one NSData stored in "A", so i won't be able to use predicate to filter and improve SELECT performances). So i decided to remove "P", and to add "A" into "C", "D"..., "G". The problem is with "R", because before i had only one inverse relationship in it, and now i need to create one each time i create a new kind of entity. So i would like to remove all the inverse relationships, is it possible? Sometimes i need to create managed object with a nil context, and i insert them into the context later, this is probably why the inverse relationship are not automatically set by core data if i set the non-inverse before the insertion in the MOC right ? Anyway i never need the inverse, so can i avoid defining them even if i get a warning ?

第二个问题,在特定情况下,我需要创建一个新R,并在MOC保存期间将其分配给C,D,..,G。所以我想使用willSave,但我不知道是否创建的实体将被保存。如果MOC对insertedObjects/updatedObjects/deletedObjects做了一个简单的循环,并且对于每个对象,它调用willSave,保存,然后调用didSave,这意味着我将修改数组它是循环,然后它应该崩溃没有?

2nd problem, in specific situations i need to create a new "R" and to assign it to "C", "D",.., "G" during the MOC save. So i would like to use willSave but, i don't know if the created entity will be saved. If the MOC does a simple loop over the "insertedObjects" / "updatedObjects" / "deletedObjects", and for each object it calls willSave, does the save, and then calls didSave, it means that i'm going to modify the array on which it is iterating, and then it should crash no?

推荐答案

由于你和我都独立发现了硬的方式,非常,非常小心与从彼此继承的实体。我也发现Core Data倾向于创建一个巨型表,其中包含基本实体的所有字段以及从中派生的所有实体,因此任何给定实体都包含来自其最远祖先的每个潜在实体的字段。非常,非常慢和昂贵。

As you and I both independently found out the hard way, be very, very careful with entities which inherit from each other. I too found Core Data tends to make one giant table containing all the fields for the base entity and all entities which derive from it, so any given entity contains the fields for every potential entity from its furthest ancestor downwards. Very, very slow and expensive.

我强烈建议只有实体类本身从彼此继承,并反映所有实体中的基类的属性,而不是托管对象模型本身中的任何实际继承。

I highly recommend only having the entity classes themselves inherit from each other, and mirroring the properties of the base class in all entities instead, without any actual inheritance in the managed object model itself.

这篇关于核心数据继承vs无继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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