具有多对多关系的 NSSortDescriptor [英] NSSortDescriptor with to-many relationship

查看:22
本文介绍了具有多对多关系的 NSSortDescriptor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父实体,它有多个子实体.如果我使用属性(例如名称)订购 Parent fetch,一切正常.但如果我试试这个:

I have a Parent entity which has to-many Child. If I order the Parent fetch with a property (e.g. name), everything works fine. But if I try this:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"child.@count" ascending:NO];

它不起作用.我也尝试添加

It doesn't work. I've also tried to add

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:[entityProperties objectForKey:@"child"]]];

但效果不佳.

我是否可以按对多排序.@count 还是我需要添加一个属性并在每次添加一个孩子时更新它?

Am I allowed to sort by to-many.@count or do I need to add a property and update it each time I add a child?

推荐答案

假设您使用的是 sqlite 存储;您不能按 @count 排序,因为它是在对象"级别解析的,而 NSFetchRequest 上的 NSSortDescriptor 是在数据库级别解析的.因此,如您所料,您需要创建一个计数属性来进行排序.

Assuming you are using a sqlite store; you cannot sort by @count because it is resolved at the "object" level whereas the NSSortDescriptor on the NSFetchRequest is resolved at the database level. Therefore, as you guessed, you would need to create a counting property to sort by.

如果您使用的是二进制或内存存储类型,那么 count 应该可以正常工作,因为它们只能作为对象访问.

If you are using a binary or in-memory store type then count should work fine since they are only accessed as objects.

这篇关于具有多对多关系的 NSSortDescriptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆