使用 NSFetchedResultsController 按可转换属性对核心数据对象进行排序

发布于 2024-10-29 02:24:29 字数 703 浏览 0 评论 0原文

我正在 UITableView 中显示存储在 Core Data 中的对象,并且在按对象的可转换属性之一对这些对象进行排序时遇到问题。我应该指出,我使用 NSFetchedResultsController 作为核心数据存储和表视图之间的控制器。当我只是使用数组来保存所有对象时,我可以毫无问题地对它们进行排序。我使用 FRC 是因为我需要将数据分组到带有节标题的节中,而 FRC 使这变得非常容易。

我们将这些对象称为我正在排序的“测量”对象。每个测量对象都有一个距离属性。该距离属性属于自定义类 EPHDistance,因此它在核心数据模型中设置为 Transformable 属性。

长话短说,按距离对测量对象进行排序确实有效,但只有在我编辑了由 Core Data 存储的对象或向存储中添加新对象后才有效。编辑商店并返回到按顺序列出所有测量对象的表格后,一切都运行良好。这只是表视图的初始启动和查看,其中对象未正确排序。实际上,我已经在 EPPDistance -compare: 方法中放置了一条 NSLog 语句,并且当我对对象进行排序时,它不会被调用,直到我在核心数据存储中添加/编辑对象为止。无论如何,如果我按“日期”属性(即 NSDate)对这些Measurement对象进行排序,那么它一开始就可以很好地工作。

我对 Core Data 并不是很有经验,这是我第一次真正尝试使用 NSFetchedResultsController,所以我对此有点困惑。任何意见将不胜感激。

多谢, 埃里克

I'm displaying objects stored in Core Data in a UITableView and am having problems sorting these objects by one of the object's transformable attributes. I should point out that I'm using an NSFetchedResultsController as the controller between the Core Data store and my table view. When I was simply using an array to hold all of my objects, I could sort them without any problems at all. I'm using an FRC because I need the data grouped in sections with section headers and the FRC makes that very easy.

Let's call these objects I'm sorting "Measurement" objects. Each Measurement object has a distance attribute. That distance attribute is of a custom class, EPHDistance, so it's set up in the Core Data model as a Transformable attribute.

To make a long story short, the sorting of Measurement objects by their distance does work, but only after I've edited an object that's stored by Core Data or if I add a new object to the store. After editing the store and returning to my table that lists all the Measurement objects in order, everything works great. It's just the initial launch and viewing of the table view where the objects aren't sorted properly. I've actually placed an NSLog statement in my EPPDistance -compare: method and it's not getting called when I sort the objects until I add/edit an object in the Core Data store. For what it's worth, if I sort theses Measurement objects by their "date" attribute, which is an NSDate, it works great right out of the gate.

I'm not super experienced with Core Data and this is my first real attempt at using an NSFetchedResultsController so I'm a little baffled by this. Any input would be greatly appreciated.

Thanks a lot,
Erik

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

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

发布评论

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

评论(1

罪歌 2024-11-05 02:24:29

您可以在 Measurement 类中创建一个可选方法,调用 -(NSString*)distanceCompareString,它返回一个字符串,帮助您从 EPHDistance 中进行排序> 对象。在您的 NSSortDescriptor 中,您只需使用 distanceCompareString 作为排序键。

You could create an optional method in your Measurement class call -(NSString*)distanceCompareString, which returns a string that will help you sort from your EPHDistance object. The in your NSSortDescriptor, you just use distanceCompareString as your sort key.

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