NSSortDescriptor with to-many relationship

风格不统一 提交于 2020-02-10 11:39:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7839265/nssortdescriptor-with-to-many-relationship

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!