2010-07-27 79 views
6

我正在学习核心数据,特别是关于聚合的工作。如何计算coredata(聚合)?

当前我想要做的事情:统计表格中的记录数,这个记录在一些准则中处于对数关系。从杰夫lemarche

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"]); 

它:

目前我做这个。

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

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]; 

据工作很好。但我想在同一时间做这种类型的多个请求。所以我认为这不可能成为计数的首选方式。

编辑

所以我觉得做法是适当的一个????

所以任何人都可以告诉我更有效的做这个的首选方法。

谢谢。

回答

4

Jeff LaMarche只是用它作为一个简单的例子。在实践中,这种需求非常普遍,Key-Value Coding具有内置的宏来处理它和其他常见的集合操作。

请参见:The Key-Value Programming Guide: Set and Array Operators

在这种情况下,你会使用@count运营商在你的谓语。

当然,调整自己的表达式可以很好地控制谓词,但操作符处理80%的任务。

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; 
}