-4

In my Core Data application I have only one table like this:

enter image description here

What I want is a query which returns all rows group by subject in the ascending order of date, ie Required output

I am not familiar with Core Data predicates. How can I accomplish this?

Jeff
  • 1,405
  • 3
  • 26
  • 46
  • http://stackoverflow.com/questions/13667288/how-to-sort-nsmutablearray-of-date-objects – Mital Jul 19 '13 at 05:27
  • Please refere below link,it may help you. http://stackoverflow.com/questions/14942912/how-to-group-by-day-in-nsdate-with-nspredicate-needed-to-create-multiple-uitab – Mital Jul 19 '13 at 05:33
  • `SELECT * FROM YOUR TABLE_NAME GROUP BY Subject WHERE Date ASC;` – Hemang Jul 19 '13 at 09:47
  • @Jeff why minus vote? i tried to help you by giving some example. – Kundan Jul 19 '13 at 11:10
  • @Hemang When I used your code app crashes with following exception *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELECT * FROM Message GROUP BY Subject WHERE Date ASC"' – Jeff Jul 19 '13 at 14:17

1 Answers1

2

you can write the code like this :-

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Subject == 'Leave'"];

you will get the list of Subjects with title Leave.Now you can Modify the code as per your requirement.

This will help you to get them in ascending order:-

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

For references:-https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html

Kundan
  • 3,084
  • 2
  • 28
  • 65