核心数据计数(或不同)

时间:2013-03-12 00:31:27

标签: ios objective-c core-data

我有一张约100行的表格。其中一列有一个名为“MeetingType”的列,它始终是一个int值(1,2或3)

我怎样才能创建一个获取谓词,该谓词将返回有多少行具有不同类型的计数?

例如,如果在100行中,50表示类型= 0而25表示类型= 1,其余25表示类型= 2。结果集的计数为3.(显示所有行中有3种不同的类型)

3 个答案:

答案 0 :(得分:4)

这是简单而纯粹的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];

答案 1 :(得分:1)

将结果作为一组获取,然后计算该组中的项目数。

编辑#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

}

答案 2 :(得分:1)

在集合中计算的替代方法,你可以做三次计数。

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属性的对象快得多。