具有唯一约束的一对一关系的合并策略会删除对象而不是更新它

发布于 2025-01-16 01:04:14 字数 1251 浏览 2 评论 0原文

我刚刚遇到了奇怪的核心数据行为。对于上下文,我的应用程序通过使我的各种核心数据实体可解码来使用 JSON API。用户可以跟踪他们感兴趣的电视节目,因此我有一个与 Season 具有一对多关系的 Show 实体。相反的是一对一的关系,因为一个季节只能属于一个电视节目。我对 ShowSeason 实体的 remoteID 属性有一个独特的约束。

在发布时,我请求用户电视节目的更新。由于我提取数据的方式,JSON 响应本质上会创建重复项,然后在保存时,由于唯一约束,NSMergePolicy.mergeByPropertyObjectTrump 将确保重复项被合并。

这对于常规属性和多对多关系来说效果很好,但现在我正在同步季节,一对一关系会删除与其他所有同步的季节。例如:

同步 1:

  • Show A 在本地有 4 季。
  • Show A 共 4 季。
  • 保存上下文后,Show A 现在有 0 个季节

同步 2:

  • Show A 现在在本地有 0 个季节。
  • Show A 共 4 季。
  • 保存上下文后,Show A 现在有 4 个季节

请注意,如果我将逆关系设置为一对多(从 SeasonShow) ,这种行为不会发生。这对于我的目的来说是可以的,但并不理想,因为它可能会产生问题。

这是我对所发生情况的解释:

  • 核心数据首先合并重复的 Season 对象。
  • 由于Season 只能有 1 个Show,因此重复的Show 之一将失去与该Season 的链接。
  • 然后,Core Data 合并重复的 Show 对象。
  • 那时,其中一个有季节,而另一个则没有。
  • Core Data 认为没有季节的变化是最近的“变化”,因此应该提交给商店。

最后一点我不确定并且对我来说不太有意义。我想我的问题是:有人确切地知道这里发生了什么并且可以证实或更正我的解释吗?另外,如果有什么办法我不知道,我将非常感激!

I've just encountered an odd Core Data behaviour. For context, my app consumes a JSON API by making my various Core Data entities decodable. Users can track TV Shows they're interested in so I have a Show entity with a to-many relationship to Season. The inverse is a to-one relationship since a season can only belong to a single TV show. I have a uniquing constraint on a remoteID property for both Show and Season entities.

At launch, I request updates on the user's TV shows. Due to the way I ingest the data, the JSON response essentially creates duplicates, then on save the NSMergePolicy.mergeByPropertyObjectTrump would ensure duplicates are merged thanks to the uniquing constraint.

This has been working fine for regular properties and to-many relationships but now that I'm syncing seasons, the to-one relationship deletes seasons with every other sync. For example:

Sync 1:

  • Show A has 4 seasons locally.
  • Show A is fetched with 4 seasons.
  • After context is saved Show A now has 0 seasons

Sync 2:

  • Show A now has 0 seasons locally.
  • Show A is fetched with 4 seasons.
  • After context is saved Show A now has 4 seasons

Note that if I set my inverse relationship to be to-many (from Season to Show), this behaviour does not happen. It's ok for my purposes but not ideal as it could create issues down the line.

This is my interpretation of what happens:

  • Core Data first merges the duplicated Season objects.
  • Because a Season can only have 1 Show, one of the duplicated Show will lose its link to that Season.
  • Core Data then merges the duplicated Show objects.
  • At that point, one of them has seasons and the other one doesn't.
  • Core Data decides that the one without seasons is the most recent "change" and thus the one that should be committed to the store.

That last point I am unsure about and doesn't quite make sense to me. I guess my question is: does anyone knows precisely what is happening here and could confirm or correct my explanation? Also, if there's any way around it I don't know, I'd be extremely grateful!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文