核心数据:在一对多关系中按计数排序

发布于 2024-09-26 23:32:43 字数 601 浏览 4 评论 0原文

我目前正在尝试设置一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↘紸啶 2024-10-03 23:32:43

从 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 expression children.@count and then set fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"childCount" ascending:NO]]

行雁书 2024-10-03 23:32:43

据我所知,您无法在查询中应用@count,但您可以轻松地将其应用到获取的数组。

NSEntityDescription * entity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"children.@count" ascending:NO];
NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSArray *sortedArray  = [results sortedArrayUsingDescriptors:descriptors];

AS far as I know you can't apply the @count in the query, but you can easily apply it to the fetched array.

NSEntityDescription * entity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"children.@count" ascending:NO];
NSArray *descriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSArray *sortedArray  = [results sortedArrayUsingDescriptors:descriptors];
錯遇了你 2024-10-03 23:32:43

我将 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 my NSManagedObject subclass, as all the magic seems to happen under the hood.

So in this particular case, add an attribute countOfChildren to the Parent entity. This should remove the exception.

EDIT: This fix seems to only work on iOS 6.1, not on iOS 6.0.

月棠 2024-10-03 23:32:43

我最终在我的实体中添加了一个计数属性,每当添加新关系时我都会增加它。使 fetchedResultsController 更易于使用。

如果您找到了使用 KVC 的方法,请感兴趣。我从来不需要修复这个黑客行为。

像这样的东西可能有用:

entity *match;    
match = (entity *)[objects objectAtIndex:0];
        NSSet *t = [match valueForKey:@"entity"]; 
        if(![t containsObject:newEntity]){
            int newCount = [match.count intValue] +1;
        [match addEntityObject:newEntity];
            [match setCount:[NSNumber numberWithInt:newCount]];

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:

entity *match;    
match = (entity *)[objects objectAtIndex:0];
        NSSet *t = [match valueForKey:@"entity"]; 
        if(![t containsObject:newEntity]){
            int newCount = [match.count intValue] +1;
        [match addEntityObject:newEntity];
            [match setCount:[NSNumber numberWithInt:newCount]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文