核心数据使用组按名称获取属性

时间:2014-03-19 07:02:56

标签: ios iphone ipad core-data

这是我想为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;

}

1 个答案:

答案 0 :(得分: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;
    }