【问题标题】: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;

}

【问题讨论】:

标签: ios iphone ipad core-data


【解决方案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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2023-04-02
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多