如何计算coredata(聚合)?

har*_*alb 6 iphone core-data count aggregation

我正在学习核心数据,特别是在聚合方面.

当前我想做的事情:在一些标准上计算表中与多对关系的记录数与反向关系.

目前我这样做:

NSExpression *ex = [NSExpression expressionForFunction:@"count:" 
                                                 arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"ddname"]]];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
    NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
    [ed setName:@"countDDEvents"];
    [ed setExpression:ex];
    [ed setExpressionResultType:NSInteger16AttributeType];
    NSArray *properties = [NSArray arrayWithObject:ed];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPredicate:pred];
    [request setPropertiesToFetch:properties];
    [request setResultType:NSDictionaryResultType];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
    [request setEntity:entity];
    NSArray *results = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:nil]; 
    NSDictionary *dict = [results objectAtIndex:0];
    NSLog(@"Average birthdate for female heroes: %@", [dict objectForKey:@"countDDEvents"]);
Run Code Online (Sandbox Code Playgroud)

它来自jeff lemarche.

编辑:我找到了我的解决方案

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
    [request setPredicate:pred];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
    [request setEntity:entity];

    NSError *error = nil;
    NSUInteger count = [[self.currentAccount managedObjectContext] countForFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)

它工作得很好.但我想一次做更多这种类型的请求.所以我认为这不是获得计数的首选方式.

编辑:

所以我认为这种方法是合适的?

所以任何人都可以告诉我更有效率的首选方式.

谢谢 .

小智 5

我不得不计算大约10 000个实体,并且在使用countForFetchRequest执行此操作时,它的界面响应速度大大降低了.

这是与NSExpression一起做的一种方式:

- (NSUInteger) unfilteredFCsCount {

// Just the fetchRequest
    NSFetchRequest *fetchRequest = [self unfilteredFCsFetchRequest];

    [fetchRequest setResultType: NSDictionaryResultType];

// You can use any attribute of the entity. its important, because you are not counting 
// the properties, but actually the entities
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"sortIndex_"]; // Does not really matter
    NSExpression *maxExpression = [NSExpression expressionForFunction: @"count:" 
                                                            arguments: [NSArray arrayWithObject:keyPathExpression]];
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName: @"fcCount"];
    [expressionDescription setExpression: maxExpression];
    [expressionDescription setExpressionResultType: NSInteger32AttributeType];

    [fetchRequest setPropertiesToFetch: [NSArray arrayWithObject:expressionDescription]];

    NSUInteger fcCount = 0;
    NSError *error = nil;
    NSArray *results = nil;
    results = [self.managedObjectContext executeFetchRequest: fetchRequest error: &error];
    KSLog(KSLogLevelDebug, @"unfilteredFCsCount results: %@", results);

    if([results count] > 0) {
        NSNumber *count = [[results objectAtIndex: 0] objectForKey: @"fcCount"];
        fcCount = [count intValue];
    }

    return fcCount;
}
Run Code Online (Sandbox Code Playgroud)


Tec*_*Zen 4

Jeff LaMarche 只是用这个作为一个简单的例子。在实践中,这种需求非常普遍,以至于键值编码有一个内置宏来处理它和其他常见的集合操作。

请参阅:键值编程指南:集合和数组运算符

@count在这种情况下,您将在谓词中使用运算符。

当然,手动调整您自己的表达式可以让您很好地控制谓词,但运算符处理 80% 的此类任务。