核心数据:在一对多关系中按计数排序
我目前正在尝试设置一个 NSFetchedResultsController ,它将根据一对多关系中的实体数量对表视图进行排序。我不确定这在计数时是否会产生影响,但这也是一种反比关系。
我认为这样的事情会很好地工作:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parent"
inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]
initWithKey:@"children.@count" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,
nil];
[fetchRequest setSortDescriptors:sortDescriptors];
我不断收到“包含 KVC 聚合的 Keypath,其中不应该有一个;”无法处理孩子。@count'。
关于可能出什么问题的任何想法吗?
I am currently trying to setup a NSFetchedResultsController that will order my table view based on the number of entities in a to-many relationship. I am not sure if this makes a difference when counting, but this is also an inverse relationship.
I thought something like this would work just fine:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parent"
inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]
initWithKey:@"children.@count" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,
nil];
[fetchRequest setSortDescriptors:sortDescriptors];
I keep getting 'Keypath containing KVC aggregate where there shouldn't be one; failed to handle children.@count'.
Any ideas on what could be going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 iOS 13(以及其他版本)开始,您可以创建一个名为 < code>childCount 与派生表达式
children.@count
,然后设置fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"childCount" ascending:NO]]
Since iOS 13 (and friends) you can create a derived attribute called
childCount
with the derivation expressionchildren.@count
and then setfetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"childCount" ascending:NO]]
据我所知,您无法在查询中应用@count,但您可以轻松地将其应用到获取的数组。
AS far as I know you can't apply the @count in the query, but you can easily apply it to the fetched array.
我将 KVO 访问器
-countOf
作为整数类型的属性添加到我的托管对象模型中。我没有在我的 NSManagedObject 子类中为这个属性实现任何东西,因为所有的魔法似乎都发生在幕后。因此,在这种特殊情况下,请将属性
countOfChildren
添加到Parent
实体。这应该会消除异常。编辑:此修复似乎仅适用于 iOS 6.1,不适用于 iOS 6.0。
I added the KVO accessor
-countOf<Key>
as an attribute to my managed object model as an integer type. I did NOT implement anything for this attribute in myNSManagedObject
subclass, as all the magic seems to happen under the hood.So in this particular case, add an attribute
countOfChildren
to theParent
entity. This should remove the exception.EDIT: This fix seems to only work on iOS 6.1, not on iOS 6.0.
我最终在我的实体中添加了一个计数属性,每当添加新关系时我都会增加它。使 fetchedResultsController 更易于使用。
如果您找到了使用 KVC 的方法,请感兴趣。我从来不需要修复这个黑客行为。
像这样的东西可能有用:
i ended up adding a count property into my entity, I just increased it whenever a new relationship was added. made the fetchedResultsController easier to use.
Be interested if you found a way using KVC. I never needed to fix this hack.
something like this may be useful: