Taj rajan
Taj rajan

Reputation: 631

Core Data Fetching Properties with Group By name

This is the SQL version of the query I would like to write for Core Data:

SELECT ROUNDNAME 
FROM MODELSCHEDULEFIXTURE 
GROUP BY ROUNDNAME 
ORDER BY MATCHID

I used the below code, but the app is crashing.

-(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;

}

Upvotes: 0

Views: 177

Answers (1)

Nag Raja
Nag Raja

Reputation: 214

Try it.

 -(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;
    }

Upvotes: 1

Related Questions