我有一个大约 100 行的表。其中一列有一个名为“MeetingType”的列,它始终是一个 int 值(1、2 或 3)
我如何创建一个 fetch 谓词来返回计数 a 有多少行具有不同的类型?
例如,如果在 100 行中,50 行的类型 = 0,25 行的类型 = 1,其余 25 行的类型 = 2。结果集的计数为 3。(表明所有行中有 3 种不同的类型)
我有一个大约 100 行的表。其中一列有一个名为“MeetingType”的列,它始终是一个 int 值(1、2 或 3)
我如何创建一个 fetch 谓词来返回计数 a 有多少行具有不同的类型?
例如,如果在 100 行中,50 行的类型 = 0,25 行的类型 = 1,其余 25 行的类型 = 2。结果集的计数为 3。(表明所有行中有 3 种不同的类型)
这是简单而纯粹的NSFetchRequest
版本:
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Meeting"];
[request setResultType:NSDictionaryResultType];
[request setPropertiesToGroupBy:@[@"MeetingType"]];
[request setPropertiesToFetch:@[@"MeetingType"]];
NSError* error = nil;
NSUInteger count = [[context executeFetchRequest:request error:&error] count];
除了在集合中计数之外,您还可以进行三个计数获取。
NSError *error;
int count;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([yourObject class])];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"MeetingType == 1"];
count = [managedObjectContext countForFetchRequest:fetchRequest error:&error];
if (count > 0) {
//Add 1 to your set or handle appropriately
}
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"MeetingType == 2"];
count = [managedObjectContext countForFetchRequest:fetchRequest error:&error];
if (count > 0) {
//Add 2 to your set or handle appropriately
}
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"MeetingType == 3"];
count = [managedObjectContext countForFetchRequest:fetchRequest error:&error];
if (count > 0) {
//Add 3 to your set or handle appropriately
}
这应该比实际拉出对象并在该集合中搜索具有不同 MeetingType 属性的对象要快得多。
将结果作为一个集合获取,然后计算集合中的项目数。
编辑#2:还有以下链接可以帮助您获得更简洁的方法。http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html#//apple_ref/doc/uid/20002176-BAJEAIEE
编辑:好的,这是一个尝试。
//you will be working on some sort of entity, i've guessed its called Meeting
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meeting" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
//you can set a predicate here if you like, eg only meetings with a certain name or something
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", type_name];
[request setPredicate:predicate];
// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];
// Create an expression for the key path, in this case MeetingType
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"MeetingType"];
// Create an expression description using the maxExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"meetingTypes"];
[expressionDescription setExpression:keyPathExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
// Execute the fetch.
NSError *error = nil;
NSArray *objects = [sharedAISDataService.managedObjectContext executeFetchRequest:request error:&error];
//check for no error
if(error == nil){
//the following set will contain unique NSNumber objects
NSSet *setOfMeetinTypes = [NSSet setWithArray:objects];
int numberOfMeetinTypes = [setOfMeetinTypes count];
//you now have the number of unique meeting types
}