核心数据不会自动处理逆关系

发布于 2024-12-31 23:57:17 字数 826 浏览 0 评论 0原文

我有 3 个这样的实体:

抱歉,该图像存在问题,即“周”实体的“日”关系应该是对多的,关系末尾应该有2个箭头

抱歉,这张图片有一个问题,“周”实体的“日”关系应该是对多的,应该有2个关系结束的箭头

现在有两个问题。

第一。对于任何Day-Class(NSManagedObject的自定义子类)对象,在我关心它的关系之后,例如:

Week* aWeek;
Day *aDay;
[aWeek addDayObject:aDay];

然后我可以真正通过Week实体的关系“day”找到Day,

但相反,我发现关系“thisWeek” Day 对象“aDay”仍然为零。

我听说,在创建 NSMangedObject 的子类之后,您将获得类似的方法

- (void)addXXXObject:(xxx)xxx

来帮助您处理多对多关系,并且会自动处理反向关系。

但这里似乎不是。那么我是否必须自己处理逆向关系,

或者如果核心数据确实提供了,我怎样才能让它自动将对象添加到逆向关系上?

第二。假设有很多“天”和一个“周”,通过它们的关系连接起来,

我想要的是,每次用户删除其中一天时,我们检查该周是否仍然有一些天的关系,

如果有,只要让用户删除那一天,

如果没有,我们也删除“Week”对象。

那么我应该使用什么对多关系的删除规则呢?

我对这些规则不太清楚..

谢谢!

I have 3 entities like this:

sorry that this image has a problem that the "day" relationship of "Week" entity should be to-many, there should be 2 arrows on the end of the relationship

sorry that this image has a problem that the "day" relationship of "Week" entity should be to-many, there should be 2 arrows on the end of the relationship

now here comes 2 questions.

1st. For any Day-Class (Customized subclass of NSManagedObject) objects, after I take care about its relationships like:

Week* aWeek;
Day *aDay;
[aWeek addDayObject:aDay];

then I can truely find the Day through the Week Entity's relationship "day",

but inversely, I found that the relationship "thisWeek" of the Day object "aDay" is still nil.

I've heard that after you creat your subclass of NSMangedObject, you got methods like

- (void)addXXXObject:(xxx)xxx

to help you deal with to-many relationships and would automatically take care with the inverse relationship.

but here it seems not. So do I have to deal with the inverse relationship by my self,

or if core data does provide, how can I make it automatically add the object on the inverse relationship?

2nd. Suppose there are many "Day"s with one "Week", connected by their relationship,

what I want is , everytime the user delete one of the Days, we check whether if the Week still has some Days in relationship,

if it does, just let the user delete that Day,

if not, we also delete the "Week" object.

So what Delete Rule of the to-many relationship should I use?

I'm not so clear with these rules..

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2025-01-07 23:57:17

只要您在两个实体中定义了它,CoreData 实际上应该处理逆关系。请参阅此处。

我将使用 nullify 删除规则来表示日->周关系,并使用 级联 表示周->日关系。
当您删除该周时,与其关联的所有日期也会被删除。
当您删除某一天时,该天将从一周中删除。当一周不再有天数时,您无法自动删除该周。但是,如果 self.days.count == 0,您可以观察一周的天数并删除 self

CoreData should actually take care of the inverse relationship as long a you defined it in both entities. See here.

I would use nullify delete rule for the day->week relationship and cascade for week->day.
When you delete the week, all days associated with it would get deleted too.
When you delete a day, it will be removed from the week's days. You cannot auto-delete the week when it has no days anymore. You can, however, observe the the week's days and delete self if self.days.count == 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文