如何通过关系访问 coredata 托管对象来获取它们的有序列表?
如何通过关系访问 coredata 托管对象来获取它们的有序列表?
即:
- 具有以下实体:LIST、LIST_ITEM(包括“订单”字段)和 ITEM。
- 假设我已经获取了我想要使用的列表,
- 然后我可以使用 coredata 关系通过使用关系来获取 LIST_ITEMS:例如“list1.listItems”,然后对于每个 LIST_ITEMS 我可以获得 ITEM (" listItem1.item")
但如果我真的只想从 LIST 中得到一个有序的 ITEMS 列表,基于 LIST_ITEM 中的“Order”字段,那么最简单的方法是什么做这个?
How to obtain an ordered list of coredata managed objects via accessing them via a relationship?
That is:
- have the following entities: LIST, LIST_ITEM (includes an 'Order' field), and ITEM.
- assume that I have already fetched the list I want to work with
- I can then use the coredata relationships to get the LIST_ITEMS via using the relationship: e.g. "list1.listItems", and then for each of these LIST_ITEMS I can get the ITEM ("listItem1.item")
But if I really just want, from the LIST, an ordered list of ITEMS from the list, based on the "Order" field in the LIST_ITEM, what is the easiest way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为我正确理解了这个问题,但我认为从 CoreData 获取列表的最佳方法是创建一个复合谓词,然后以这种方式搜索项目。例如,如果我仅查找特定用户(另一个实体)在某一天的事件(实体)。然后我可以为事件条目创建一个 NSFetchRequest 并以 (user.name==%@) AND (event.date==%@) 的形式指定 NSPredicate 指定用户的姓名和日期
I think that I understand the question correctly, but I think the best way to get lists from CoreData is create a compoun predicate and then search for items that way. For example if I am looking for only Events (entity) on a certain day, for a specific User (another entity). Then I can create an NSFetchRequest for the Event entry and specify and NSPredicate in the form of (user.name==%@) AND (event.date==%@) specifying the user's name and date
您可以使用 NSSortDescriptor 对关系返回的项目进行排序,就像在常规获取请求中一样。例如:
这里的关键方法是
[NSSet SortedArrayUsingDescriptors:]
You can sort the items returned by the relationship using an NSSortDescriptor just as you would in a regular fetch request. For example:
The key method here is
[NSSet sortedArrayUsingDescriptors:]