Ios 核心数据计数(或不同)
我有一张大约有100行的桌子。其中一列有一个名为“MeetingType”的列,该列始终是int值(1、2或3) 如何创建一个fetch谓词来返回不同类型行的计数Ios 核心数据计数(或不同),ios,objective-c,core-data,Ios,Objective C,Core Data,我有一张大约有100行的桌子。其中一列有一个名为“MeetingType”的列,该列始终是int值(1、2或3) 如何创建一个fetch谓词来返回不同类型行的计数 例如,如果100行中有50行的类型为0,25行的类型为1,其余25行的类型为2。结果集的计数为3。(显示所有行中有3种不同的类型)将结果作为一个集合获取,然后计算集合中的项目数 编辑#2:还有下面的链接可以帮助您获得更简洁的方法 编辑:好的,这里有一个尝试 //you will be working on some sort of e
例如,如果100行中有50行的类型为0,25行的类型为1,其余25行的类型为2。结果集的计数为3。(显示所有行中有3种不同的类型)将结果作为一个集合获取,然后计算集合中的项目数 编辑#2:还有下面的链接可以帮助您获得更简洁的方法 编辑:好的,这里有一个尝试
//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:还有下面的链接可以帮助您获得更简洁的方法 编辑:好的,这里有一个尝试
//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
}
除了在集合中计数之外,您还可以进行三次计数取回
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属性的对象快得多。除了在集合中计数外,还可以进行三次计数获取
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属性的对象快得多。这里是简单而纯粹的
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];
以下是简单而纯粹的
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];
你能给我举个例子吗。我对核心数据,尤其是谓词是如此陌生,以至于我仍然难以从头开始创建它们。再次感谢。哦,我无法从我的头顶上获得语法正确的信息,但我应该能够给你足够的信息,让你走上正确的道路。你能给我举个例子吗。我对核心数据,尤其是谓词是如此陌生,以至于我仍然难以从头开始创建它们。再次感谢。哦,我无法从我的头顶上获得语法正确的信息,但我应该能够给你足够的信息,让你走上正确的道路。