NSSortDescriptor具有一对多关系 [英] NSSortDescriptor with to-many relationship
问题描述
我有一个父实体,它有许多孩子。如果我使用属性(例如名称)订购父提取,一切工作正常。但是如果我尝试这样:
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@child。@ count没有];
它不工作。我也试图添加
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:[entityProperties objectForKey:@child]]] ;
但是效果不好。
假设您使用的是sqlite存储;您不能按 @count
排序,因为它在对象级别解析,而 NSSortDescriptor
在 NSFetchRequest
在数据库级别解析。因此,你猜到,你需要创建一个计数属性来排序。
如果你使用二进制或内存存储类型,则count应该工作得很好因为它们只能作为对象访问。
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"]]];
But it didn't work as well.
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?
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.
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屋!