Core data 核心数据:分组依据和计数结果返回空列表

Core data 核心数据:分组依据和计数结果返回空列表,core-data,ios7,nspredicate,nsexpression,nsattributedescription,Core Data,Ios7,Nspredicate,Nsexpression,Nsattributedescription,考虑以下核心数据实体: 人员-人员ID:NSNumber,姓名:NSString,职位:NSString 使用核心数据,我尝试复制以下SQL查询: 选择'position',按'position'从'Person'组中计数(*)` 以下是objective-c等效值: NSEntityDescription*entity=[NSEntityDescription entityForName:@“Person”] NSFetchRequest*request=[NSFetchRequest fe

考虑以下核心数据实体:

人员-人员ID:NSNumber,姓名:NSString,职位:NSString

使用核心数据,我尝试复制以下SQL查询:

选择'position',按'position'从'Person'组中计数(*)`
以下是objective-c等效值:

NSEntityDescription*entity=[NSEntityDescription entityForName:@“Person”]
NSFetchRequest*request=[NSFetchRequest fetchRequestWithEntityName:@“Person”];
NSExpression*keyPathExpression=[NSExpression expressionForKeyPath:@“位置”];
NSExpression*countExpression=[NSExpression expressionForFunction:@“count:”参数:@[keyPathExpression]];
NSAttributeDescription*positionDescription=[entity.attributesByName objectForKey:@“position”];
NSExpressionDescription*expressionDescription=[[NSExpressionDescription alloc]init];
[expressionDescription setName:@“count”];
[expressionDescription setExpression:countExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
[请求setPropertiesToFetch:@[positionDescription,expressionDescription]];
[请求setPropertiesToGroupBy:@[positionDescription]];
[请求setResultType:NSDictionaryResultType];
n错误*错误=nil;
NSArray*results=[context executeFetchRequest:request error:&error];

Person实体肯定是填充的,但是,当执行上述代码时,
results
数组为空。想法?

(从我上面的评论:)带有
NSDictionaryResultType
的获取请求只返回存储文件中的对象,而不返回挂起的更改。因此,在执行该获取请求之前,必须将上下文保存到持久存储中

你得到的是空数组还是零?如果得到nil,则错误变量应包含一些信息。在执行请求之前是否保存了对象?NSDictionaryResultType的提取请求仅返回存储文件中的对象。-如果省略setPropertiesToGroupBy,您会得到结果吗?对象肯定已保存。我刚刚用您的代码运行了一个小测试应用程序,它工作并产生了预期的结果。看起来就是这样。在使用MagicalRecord和CoreData时,我没有执行MR_saveToPersistentStoreAndWait],而是使用[NSManagedObjectContext save:]操作。另外,我不知道NSDictionaryResultType的获取请求只返回存储文件中的数据。谢谢@MartinR!你能给我一个例子或者给我指一个例子吗?在这个例子中,我可以得到按特定属性分组的所有实体?