本文介绍了核心数据获取属性(按计数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我要为Core Data写入的SQL版本:
SELECT Group.Name,COUNT (Item.Name)
FROM Item INNER JOIN Group ON Item.GroupID = Group.ID
GROUP BY Group.Name
到目前为止,我有:
NSFetchRequest * fetchGroupSummary = [NSFetchRequest fetchRequestWithEntityName:@ 项目];
NSEntityDescription * entity = [NSEntityDescription entityForName:@IteminManagedObjectContext:[[CoreDataManager sharedInstance] managedObjectContext]];
NSAttributeDescription * groupName = [entity.relationshipsByName objectForKey:@group];
NSExpression * countExpression = [NSExpression expressionForFunction:@count:arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@name]]];
NSExpressionDescription * expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@count];
[expressionDescription setExpression:countExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
NSSortDescriptor * sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@group.sortascending:YES];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@group.stage ==%@,stage];
[fetchGroupSummary setEntity:entity];
[fetchGroupSummary setSortDescriptors:@ [sortDescriptor]];
[fetchGroupSummary setPropertiesToFetch:[NSArray arrayWithObjects:groupName,expressionDescription,nil]];
[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:groupName]];
[fetchGroupSummary setResultType:NSDictionaryResultType];
[fetchGroupSummary setPredicate:predicate];
NSError * error = nil
groups = [[[CoreDataManager sharedInstance] managedObjectContext] executeFetchRequest:fetchGroupSummary错误:&错误];
expressionDescription = nil;
这几乎给了我一切,但是不是groupName是组关系,我想指定组。
解决方案
setPropertiesToGroupBy of NSFetchRequest 接受 NSPropertyDescription 或 NSExpressionDescription 对象或 keypath字符串。在您的情况下,您可以使用键盘字符串:
[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:@group.name] ];
This is the SQL version of the query I would like to write for Core Data:
SELECT Group.Name, COUNT(Item.Name) FROM Item INNER JOIN Group ON Item.GroupID = Group.ID GROUP BY Group.Name
So far what I have is:
NSFetchRequest* fetchGroupSummary = [NSFetchRequest fetchRequestWithEntityName:@"Item"]; NSEntityDescription* entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[[CoreDataManager sharedInstance] managedObjectContext]]; NSAttributeDescription* groupName = [entity.relationshipsByName objectForKey:@"group"]; NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:[NSExpression expressionForKeyPath: @"name"]]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName: @"count"]; [expressionDescription setExpression: countExpression]; [expressionDescription setExpressionResultType: NSInteger32AttributeType]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"group.sort" ascending:YES]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"group.stage == %@", stage]; [fetchGroupSummary setEntity:entity]; [fetchGroupSummary setSortDescriptors:@[sortDescriptor]]; [fetchGroupSummary setPropertiesToFetch:[NSArray arrayWithObjects:groupName, expressionDescription, nil]]; [fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:groupName]]; [fetchGroupSummary setResultType:NSDictionaryResultType]; [fetchGroupSummary setPredicate:predicate]; NSError* error = nil; groups = [[[CoreDataManager sharedInstance] managedObjectContext] executeFetchRequest:fetchGroupSummary error:&error]; expressionDescription = nil;
This almost gives me everything, however instead of groupName being the group relationship I would like to specify group.name - is this possible?
Mark
解决方案
setPropertiesToGroupBy of NSFetchRequest accepts an array of NSPropertyDescription or NSExpressionDescription objects, or keypath strings. In your case, you can use a keypath string:
[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:@"group.name"]];
这篇关于核心数据获取属性(按计数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!