gpt4 book ai didi

database - 级联删除如何在 coredata 工作的多对多关系中工作?

转载 作者:搜寻专家 更新时间:2023-10-30 20:43:14 25 4
gpt4 key购买 nike

假设,我们有 2 个实体:Parentschildren

当一个parent被删除时,所有的children也会被删除。

很好。

现在我们有了名为lover的实体。

每个人都可以“爱”很多人,也可以被很多人爱。

如果个人删除会怎样?

是否级联删除:

  1. 根本不工作?
  2. 当最后一个爱你的人被删除时删除你?

或者什么?

最佳答案

我认为,你不应该以这种方式使用对多级联,因为,当它删除你删除的对象的所有 lovers 时,它可能会越来越远并删除整个数据库。

因此,对您来说更好的方法是拥有一个中间(“加入”)实体,例如,loverInfo。这是 Apple 基于 friend 关系建议您这样做的方法。

A common example of a relationship that is initially modeled as a many-to-many relationship that’s the inverse of itself is “friends”. Although it’s the case that you are your cousin’s cousin whether they like it or not, it’s not necessarily the case that you are your friend’s friend. For this sort of relationship, you should use an intermediate (“join”) entity. An advantage of the intermediate entity is that you can also use it to add more information to the relationship—for example a “FriendInfo” entity might include some indication of the strength of the friendship with a “ranking” attribute. This is illustrated here

FIgure 3

In this example, Person has two to-many relationships to FriendInfo: friends represents the source person’s friends, and befriendedBy represents those who count the source as their friend. FriendInfo represents information about one friendship, “in one direction.” A given instance notes who the source is, and one person they consider to be their friend. If the feeling is mutual, then there will be a corresponding instance where source and friend are swapped. There are several other considerations when dealing with this sort of model:

  • 要建立人与人之间的友谊,您必须创建 FriendInfo 的实例。如果两个人都喜欢对方,你必须创建 FriendInfo 的两个实例

  • 要打破友谊,你必须删除适当的实例好友信息。

  • 从Person到FriendInfo的删除规则应该是级联的。如果一个person 从商店中删除,然后是 FriendInfo 实例变得无效,因此也必须删除。
  • 作为推论,从 FriendInfo 到 Person 的关系不得是可选的——如果源或 friend 为空。

关于database - 级联删除如何在 coredata 工作的多对多关系中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11112688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com