2010-08-27 74 views
2

我有一個視圖,我希望獲得「百分比」屬性的平均值,而無需將所有對象(及其他屬性)從核心數據加載到內存中。我已經發現如何在蘋果的文檔中做到這一點,但問題是我想限制百分比被平均化的對象爲具有另一個名爲「numberAsked」的屬性大於0的對象。我認爲這是我可以做的事情與NSPredicate。在覈心數據中使用NSPredicate過濾setPropertiesToFetch?

以下是我有:

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Statistics" inManagedObjectContext:managedObjectContext]; 
[request setEntity:entity]; 

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"numberAsked > 0"]; 
[request setPredicate:searchPredicate]; 

[request setResultType:NSDictionaryResultType]; 


NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"percentCorrect"]; 
NSExpression *averagePercentExpression = [NSExpression expressionForFunction:@"average:" 
                    arguments:[NSArray arrayWithObject:keyPathExpression]]; 

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
[expressionDescription setName:@"averagePercentageCorrect"]; 
[expressionDescription setExpression:averagePercentExpression]; 
[expressionDescription setExpressionResultType:NSDecimalAttributeType]; 

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

NSError *error; 
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error]; 

什麼情況是,它似乎獲取的所有對象,而忽略了NSPredicate。我不知道是否改變請求的結果類型對此做了些什麼,或者我需要用另一個NSExpression或者什麼來過濾對象。

我的測試值是,現在所有的對象都有數字被設置爲0的屬性,所以理論上我的最終對象數組應該有0的計數,從那裏我會指定百分比的「N/A」。但是這並沒有發生。

我感謝任何幫助!

回答

2

您無法將表達式傳遞給setPropertiesToFetch:。它期望有一組NSPropertyDescription對象。我有點驚訝它沒有崩潰。

你想是這樣的:

NSManagedObject *mo; 
for (int i=0; i<5; i++) { 
    mo=[NSEntityDescription insertNewObjectForEntityForName:@"Test" inManagedObjectContext:self.moc]; 
    [mo setValue:[NSNumber numberWithInt:i] forKey:@"numAttrib" ];  
} 
[self saveContext]; 
NSFetchRequest *fetch=[[NSFetchRequest alloc] init]; 
NSEntityDescription *testEntity=[NSEntityDescription entityForName:@"Test" inManagedObjectContext:self.moc]; 
[fetch setEntity:testEntity]; 
NSDictionary *propDict=[testEntity propertiesByName]; 
[fetch setPropertiesToFetch:[NSArray arrayWithObject:[propDict valueForKey:@"numAttrib"]]]; 
NSArray *fetchReturn=[self performFetch:fetch];//<-- my custom boilerplate 
NSLog(@"fetchReturn=%@",fetchReturn); 
id theAvg=[fetchReturn valueForKeyPath:@"@avg.numAttrib"]; 
NSLog(@"theAvg=%f",[theAvg floatValue]); 

...它打印:

fetchReturn=(
    "<NSManagedObject: 0x5d2fae0> (entity: Test; id: 0x5d0c310 <x-coredata://384EFAF2-1921-4CB0-85B2-402DBA145615/Test/p1> ; data: {\n numAttrib = 3;\n})", 
    "<NSManagedObject: 0x5d35df0> (entity: Test; id: 0x5d34480 <x-coredata://384EFAF2-1921-4CB0-85B2-402DBA145615/Test/p2> ; data: {\n numAttrib = 1;\n})", 
    "<NSManagedObject: 0x5d33890> (entity: Test; id: 0x5d31810 <x-coredata://384EFAF2-1921-4CB0-85B2-402DBA145615/Test/p3> ; data: {\n numAttrib = 2;\n})", 
    "<NSManagedObject: 0x5d36d10> (entity: Test; id: 0x5d0dfa0 <x-coredata://384EFAF2-1921-4CB0-85B2-402DBA145615/Test/p4> ; data: {\n numAttrib = 4;\n})", 
    "<NSManagedObject: 0x5d38f00> (entity: Test; id: 0x5d302a0 <x-coredata://384EFAF2-1921-4CB0-85B2-402DBA145615/Test/p5> ; data: {\n numAttrib = 0;\n})" 
) 
theAvg=2.000000 

如果你想要去的,甚至更輕的重量。您可以添加:

[fetch setResultType:NSDictionaryResultType]; 

...它返回像這樣單個鍵字典的數組:

fetchReturn=(
     { 
     numAttrib = 2; 
    }, 
     { 
     numAttrib = 0; 
    }, 
     { 
     numAttrib = 4; 
    }, 
     { 
     numAttrib = 1; 
    }, 
     { 
     numAttrib = 3; 
    } 
) 
float=2.000000 
+0

我不知道這是否是我想要的東西,或者如果我很清楚... 我的代碼與下面的Apple代碼類似。我將NSExpressionDescriptions(不是NSExpressions)的數組傳遞給setPropertiesToFetch :.該代碼位於「獲取特定值」下: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484- SW6 我得到的百分比屬性的平均值就好了,只是我不一定要平均所有的百分比 - 只是該對象的另一個屬性(numAsked)> 0的對象的百分比。 – Kevin 2010-08-27 20:12:55

相關問題