Reputation: 631
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
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