[英]Data model for enum with associated values
I'm trying to figure out what's the recommended way to implement enum with associated values in Core Data data model.我试图找出在 Core Data 数据模型中使用关联值实现枚举的推荐方法是什么。 Let's say I have a book entity and I want to save in database how I got the book, like:
假设我有一个书实体,我想在数据库中保存我如何获得这本书,例如:
This would be an enum in swift:这将是 swift 中的枚举:
enum WhereItCameFrom {
case Bought(who: String, date: Date, where: String)
case Borrorwed(who: String, date: Date, dueDate: Date)
case GivenAsGift(who: String, date: Date, forWhat: String)
}
I'm thinking to implement it in data model using inheritance , as below:我正在考虑使用继承在数据模型中实现它,如下所示:
Introduce a parent entity WhereItCameFrom
and define the above cases as its children entities.引入父实体
WhereItCameFrom
并将上述案例定义为其子实体。
Define a to-one
relationship from Book
to WhereItCameFrom
.定义从
Book
到WhereItCameFrom
to-one
关系。 Its deletion rule is Cascade
.它的删除规则是
Cascade
。
Define a to-one
relationship from WhereItCameFrom
to Book
.定义从
WhereItCameFrom
到Book
to-one
关系。 Its deletion rule is Deny
.它的删除规则是
Deny
。
See the diagram:看图:
I'm wondering if this this the right way to do it and I have a few specific questions.我想知道这是否是正确的方法,我有一些具体问题。
1) What's the typical way to implement enum with associated values? 1)用关联值实现枚举的典型方法是什么?
I think my above modal is good.我认为我上面的模式很好。 But just in case, are there other better ways to do it?
但以防万一,还有其他更好的方法吗?
2) Is entity with no attributes normal? 2)没有属性的实体是否正常?
In above diagram, WhereItCameFrom
doesn't have any attributes.在上图中,
WhereItCameFrom
没有任何属性。 At first I added a type
attribute to it to indicate if it's a Bought
, Borrowed
, or GivenAsGift
entity.起初,我向它添加了一个
type
属性来指示它是Bought
、 Borrowed
还是GivenAsGift
实体。 But then I realized this information is implicit in its child entity class type, so I removed it.但后来我意识到这个信息隐含在它的子实体类类型中,所以我删除了它。 So the only purpose of the parent entity is to hold the relationship.
所以父实体的唯一目的是保持关系。 Is this use typical in Core Data?
这种用法在 Core Data 中是典型的吗?
3) Will the old object be removed automatically when modifying relationship at run time? 3) 运行时修改关系会自动删除旧对象吗?
Suppose I modify book.whereItCameFrom
relationship value at run time.假设我在运行时修改
book.whereItCameFrom
关系值。 Its previous value is a Borrowed
object.它以前的值是一个
Borrowed
对象。 Its new value is a GivenAsGift
object.它的新值是
GivenAsGift
对象。 Do I need to delete the Borrowed
object manually (I mean, doing that explicitly in application code)?我是否需要手动删除
Borrowed
对象(我的意思是,在应用程序代码中明确地这样做)?
I guess I should do it.我想我应该这样做。 But given Core Data is a framework helping to maintain data consistency in object graph, that seems awkward to me.
但是鉴于 Core Data 是一个有助于保持对象图中数据一致性的框架,这对我来说似乎很尴尬。 I wonder if Core Data has some feature that can figure out the
Borrowed
object is not needed and delete it automatically?我想知道 Core Data 是否有一些功能可以找出不需要的
Borrowed
对象并自动删除它?
Thanks for any help.谢谢你的帮助。
UPDATE:更新:
In the third question, after the old Borrowed
object is disconnected with Book
object, is my understanding correct that, from the Borrowed
object perspective, the peer object has been delete and hence the peer object's Cascade
deletion rule is applied to the Borrowed
object?第三个问题,在旧的
Borrowed
对象与Book
对象断开连接后,我的理解是否正确,从Borrowed
对象的角度来看,peer对象已经被删除,因此peer对象的Cascade
删除规则应用于Borrowed
对象? If so, then it will be deleted automatically.如果是这样,那么它将被自动删除。 I think the real question here is if deletion rule applies to relationship update or not.
我认为这里真正的问题是删除规则是否适用于关系更新。 I'll do some experiments on this later today.
我将在今天晚些时候对此做一些实验。
A few thoughts...一些想法...
1) What's the typical way to implement enum with associated values?
1)用关联值实现枚举的典型方法是什么?
I think my above modal is good.
我认为我上面的模式很好。 But just in case, are there other better ways to do it?
但以防万一,还有其他更好的方法吗?
I can't comment on typical ways of implementing enums with associated values, but your model seems to make sense.我无法评论使用关联值实现枚举的典型方法,但您的模型似乎有意义。 One word of caution: if you search StackOverflow for questions regarding entity inheritance, you will find several answers advising against using it.
一个警告:如果您在 StackOverflow 中搜索有关实体继承的问题,您会发现一些建议不要使用它的答案。 The way CD implements subentities (at least for SQLite stores) is to add all the attributes of all the subentities to the parent entity SQLite table.
CD 实现子实体的方式(至少对于 SQLite 存储)是将所有子实体的所有属性添加到父实体 SQLite 表中。 That's handled for you "under the hood" by CoreData, but the SQLite table can potentially end up being very "wide", which can affect performance.
这由 CoreData 在“幕后”为您处理,但 SQLite 表可能最终会变得非常“宽”,这会影响性能。 I've never found it an issue, but you might want to have that in mind if you have lots of data and/or the entities are more complex than you indicate in the question.
我从来没有发现它是一个问题,但是如果您有大量数据和/或实体比您在问题中指出的更复杂,您可能希望记住这一点。 Subentities can also cause issues in some rare situations - for example, I've seen questions indicating problems with uniqueness constraints.
在一些罕见的情况下,子实体也会导致问题 - 例如,我看到一些问题表明存在唯一性约束问题。
2) Is entity with no attributes normal?
2)没有属性的实体是否正常?
It's unusual, but not a problem.这是不寻常的,但不是问题。 However, as all three subentities have
date
and who
attributes, it would be wise to move these from the subentities to the parent WhereItComeFrom
entity.但是,由于所有三个子实体都有
date
和who
属性,因此将这些从子实体移动到父WhereItComeFrom
实体是明智的。 (Otherwise, as noted above, your parent entity table will have three columns for date
(one for each subentity) and three for who
). (否则,如上所述,您的父实体表将包含三列
date
(每个子实体一列)和三列who
)。
3) Will the old object be removed automatically when modifying relationship at run time?
3) 运行时修改关系会自动删除旧对象吗?
No. If you modify the book.whereItCameFrom
relationship value at run time, with a GivenAsGift
object replacing a Borrowed
object, CD's graph management will ensure that the Borrowed
object's book
property is set to nil.否。如果您在运行时修改
book.whereItCameFrom
关系值,用GivenAsGift
对象替换Borrowed
对象,CD 的图形管理将确保Borrowed
对象的book
属性设置为 nil。 The cascade rule does not prevent objects being "orphaned" in this way and you must manually delete the Borrowed
object.级联规则不会阻止对象以这种方式“孤立”,您必须手动删除
Borrowed
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.