按名称分组的核心数据获取属性
Posted
技术标签:
【中文标题】按名称分组的核心数据获取属性【英文标题】:Core Data Fetching Properties with Group By name 【发布时间】:2014-03-19 07:02:55 【问题描述】:这是我想为Core Data
编写的查询的 SQL 版本:
SELECT ROUNDNAME
FROM MODELSCHEDULEFIXTURE
GROUP BY ROUNDNAME
ORDER BY MATCHID
我使用了下面的代码,但是应用程序崩溃了。
-(NSArray *)getRound
NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest *fetch = [NSFetchRequest new];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"modelschedulefixture" inManagedObjectContext:context];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"roundName"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
NSArray *array= [context executeFetchRequest:fetch error:&error];
return array;
【问题讨论】:
可能重复 ***.com/questions/4865686/… 看看那里发布的链接。 【参考方案1】:试试看。
-(NSArray *)getRound
NSArray *array=nil;
NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"modelschedulefixture"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"modelschedulefixture" inManagedObjectContext:context];
NSSortDescriptor *sortDescreptorRound =[[NSSortDescriptor alloc]initWithKey:@"matchid" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptorRound]];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"roundname"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
array = [context executeFetchRequest:fetch error:&error];
return array;
【讨论】:
以上是关于按名称分组的核心数据获取属性的主要内容,如果未能解决你的问题,请参考以下文章