2010-07-27 101 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; 
}