当顺序很重要时,寻找一种优雅的方式在 coredata 中存储一对多关系

发布于 2024-08-25 17:17:55 字数 421 浏览 4 评论 0原文

我一直在想办法解决我的问题,但我能想到的每一个解决方案都很混乱,让我想干呕。

我有一个一对多的关系,由一个可以有许多成员对象的团队对象组成。当我使用 Xcode 构建数据模型时,我得到了默认的 NSSet 来存储成员对象,不幸的是,集合没有排序,我需要保留成员对象的顺序,并且我需要知道之间是否有空格会员们。

我想到使用 NSArray 代替 NSSet 并在我的数据存储中创建一个虚拟 Member 对象,我可以用它来标记 Member 对象之间的空位,但该解决方案对我来说确实感觉太麻烦了。因为我总是必须从任何查询中过滤掉这个虚拟成员。

NSDictionary 将是完美的,因为我可以将成员对象引用及其位置存储为对象键对(同时处理顺序和空缺),但显然 CoreData 不支持 NSDictionary。

有没有人有类似的需求,并设计了一个简单的解决方案?

I've been trying to come up with a way to solve my problem, but every solution I can think of is messy and makes me want to retch.

I have a one-to-many relationship, consisting of a Team object that can have many Member objects. When I built my data model using Xcode, I was given the default NSSet in which to store the member objects, Unfortunately Sets are not ordered and I need to preserve the order of the Member objects and I need to know if there are empty spaces between Members.

I thought of Using an NSArray in place of the NSSet and creating a dummy Member object in my data store that I could use to mark vacant a spot between to Member objects, but that solution really feels like too much of a hack to me. Since I'll always have to filter out this dummy Member from any queries.

An NSDictionary would be perfect as I could store the Member object references and their positions as Object-Key pairs, (taking care of both order and vacancies) but apparently CoreData does not support NSDictionary.

Has anyone had a similar need, and devised a simple solution?

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

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

发布评论

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

评论(2

小兔几 2024-09-01 17:17:55

有序关系的实现很简单。 这是我为 NSManagedObject 子类之一所做的代码。

(出于某种原因,我无法将格式化代码发布到今天的 Stackoverflow。)

在这种情况下,我设置了一个关系,如下所示:

AlphaEnity<-->>AlphaToBetaEntity<<-->BetaEntity

AlphaToBetaEntity 有一个 order 属性。我对 AlphaToBetaEntity 进行排序,然后根据需要返回链接的 BetaEntity。这就像在老式 C 语言中进行指针排序一样。

它的优点是在非常大的集合上速度极快,并且在需要从中读取属性之前,您不必在 BetaEntity 中出错。

Ordered relationships are trivial to implement. Here's the code I did for one of NSManagedObject subclasses.

(For some reason, I can't post formatted code to Stackoverflow today.)

In this case I've got a relationship set up like:

AlphaEnity<-->>AlphaToBetaEntity<<-->BetaEntity

The AlphaToBetaEntity has an order Attribute. I sort the AlphaToBetaEntity and then return the linked BetaEntity as needed. It's like doing a pointer sort in old school C.

It has the advantage of being blistering fast on very large sets and you don't have to fault in the BetaEntity until you need to read an attribute from it.

一个人练习一个人 2024-09-01 17:17:55

缺乏有序的核心数据对多关系是底层数据库设计的一个产物。 此中讨论了其原因以及潜在的解决方案Cocoa 邮件列表线程。解决方案包括向托管对象添加索引属性或在链接列表中维护这些对象。

如果您不想自己执行此操作,Brian Webster 已经整合了有序对多核心数据关系的实现 此处

最后,如果您想放弃 Core Data,Aaron Hillegass 的新BNRPersistence 框架支持有序关系。

The lack of ordered Core Data to-many relationships is an artifact of the underlying database design. The reasons for this, along with potential solutions, are discussed in this Cocoa mailing list thread. Solutions include adding an index property to your managed objects or maintaining these objects in a linked list.

If you don't want to do this yourself, Brian Webster has put together an implementation of ordered to-many Core Data relationships here.

Finally, if you are looking to switch away from Core Data, Aaron Hillegass' new BNRPersistence framework supports ordered relationships.

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