核心数据中的多个逆关系

发布于 2024-12-02 08:37:52 字数 343 浏览 0 评论 0原文

我有一个实体图书馆,包含两个图书列表。图书馆维护这两个图书清单非常重要。在我的图书馆实体上,我有一个从每个列表到我的图书实体的一对多关系。同样,Book也有一个关系“图书馆”。我的数据从数据库中删除时遇到一些问题,我读到我应该设置反向关系以帮助保证数据完整性。然而,在这种情况下,一本书希望能够与我的图书馆实体上的每个列表建立反向关系。我怎样才能做到这一点?

我天真的第一个想法是实现两个列表的关系。因此,一本书有一个关系“libraryForList1”和“libraryForList2”,这样每个关系都可以有一个逆关系。我永远不必实际引用这些属性,因为根据核心数据规范,如果我将一本书添加到其中一个图书馆列表中,它会自动将图书馆设置为该图书的所有者。

I have an entity Library that mains two lists of Books. It is important that the library maintain these two lists of books. On my Library entity I have a relationship thats one to many from each list to my Book entity. Likewise, Book has a relationship "library". I'm having some problems with my data being erased from the database and I read that I should be setting up inverse relationships to help with data integrity. In this case however, a Book would want to be able to set up an inverse relationship with each list on my Library entity. How can I accomplish this?

My naive first thought is to implement relationships for both lists. So a book has a relationship "libraryForList1" and "libraryForList2" so that it can have an inverse for each relationship. I'll never have to actually reference these properties because according to the Core Data spec, if I add a book to one of the library lists, it automatically takes care of setting the library as that books owner.

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

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

发布评论

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

评论(1

萌化 2024-12-09 08:37:52

您的“天真的第一个想法”本质上是正确的:如果您的图书馆实体与图书有两个一对多的关系(为了清楚起见,我们将关系称为“ownedBooks”和“borrowedBooks”),那么您的 Book 实体应该具有一对一的逆关系(owningLibraryborrowingLibrary),然后 Core Data 将使您的生活更轻松。

您可能还需要考虑这些关系的删除规则:如果由于某种原因删除了一本书,则 owningLibraryborrowingLibrary 的删除规则可能是 无效 – 也就是说,两个图书馆都会从其列表中删除该书。删除仍然拥有图书的图书馆似乎是一个坏主意,因此也许 ownedBooksborrowedBooks 的删除规则应该是拒绝:图书馆可以在所有书籍都被清点(并从图书馆中删除)之前不会被删除。

Your "naive first thought" is essentially correct: If your Library entity has two one-to-many relationships with Books (for clarity let's call the relationships ownedBooks and borrowedBooks), then your Book entity should have to-one inverse relationships (owningLibrary and borrowingLibrary) and then Core Data will make your life easier.

You may also need to think about the delete rules on these relationships: If a Book is deleted for some reason, the delete rules for the owningLibrary and borrowingLibrary would likely be Nullify – that is, both libraries would remove the Book from their lists. Deleting a Library that still has Books seems like a bad idea, so maybe the delete rule for ownedBooks and borrowedBooks should be Deny: A Library can't be deleted until the books are all accounted for (and removed from the Library).

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