2011-03-03 71 views
0

我有一個實體,它與另一個實體有許多關係。在第二個中,我有一個名爲「versionNumber」的屬性。我對實體類型A有一個對象,並且我想獲取具有最大(最大)versionNumber的相關實體B.(核心數據)獲取具有最大屬性的特定實體

我有以下的,但返回我過去對實體B的所有記錄獲得的結果,而不是對涉及A型的對象

NSInteger vNumber = 0; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:DPA_VERSION_KEY inManagedObjectContext:[self managedObjectContext]]; 
[request setEntity:entity]; 

// Specify that the request should return dictionaries. 
[request setResultType:NSDictionaryResultType]; 

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:VERSION_NUMBER_KEY]; 
NSExpression *maxNumberExpression = [NSExpression expressionForFunction:@"max:" 
                   arguments:[NSArray arrayWithObject:keyPathExpression]]; 

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






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

// Execute the fetch. 
NSError *error = nil; 
NSArray *objects = [[self managedObjectContext] executeFetchRequest:request error:&error]; 
if (objects == nil) { 
    // Handle the error. 
} 
else { 
    if ([objects count] > 0) { 
     vNumber = [[[objects objectAtIndex:0] valueForKey:@"maxNumber"] integerValue] +1; 
    } 
} 

[expressionDescription release]; 
[request release]; 

return vNumber; 

我有一個想法,但我hadn具體的實體」不能實現它。我必須問SELF哪一個是我的對象A來完成它與VVersions(實體B)的關係。 感謝您的幫助。

G.

+0

Uhmmm,肯定沒問題的關係的乙對象。 – TechZen 2011-03-03 04:09:59

+0

??????????????? – Gustavo 2011-03-03 08:46:03

回答

0

設置一個謂詞來請求限制爲僅誰擁有A.

[request setPredicate:[NSPredicate predicateWithFormat:@"myA == %@", myA]; 
相關問題