0
votes

I've been going around this question Here

And trying to implement my own solution. Somehow, I don't get any sections. Only rows.

3 rows in this case... But no Sections. There should be 2 sections at least. 2 Months. I know this because I inspected the model and they are there.

My Core Data Model has an entity Year, containing many months (Entity Month). And Month has also many days (Entity Day)

The attributes that represent each one individualy are, year_, month_ and day_, for the entities Year, Month and Day, respectively.

What I'm trying to do is a fetch request to all days, in a given Month, in a Given Year in a way that I can have my sections representing a Month and Rows representing Days.

Can someone help me out?

edit: updated code

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];


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

[fetchRequest setSortDescriptors:sortDescriptors];


[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:managedObjectContext
                                      sectionNameKeyPath:@"month.month_"
                                               cacheName:@"Root"];

My model is looking like this,

enter image description here

Any help is greatly appreciated.

Thank you!

Nuno

2
What's stopping you from using sectionNameKeyPath based on the month name, with the primary sort descriptor being the order number of the month - Carl Veazey
Doesn't work. sectionNameKeyPath:@"month.month_" and initWithKey:@"month.month_" it doesn't get me any sections, whatsoever. Only the 3 rows that I already have. - nmdias

2 Answers

1
votes
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"";

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"Month.month_" ascending:NO];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"day_" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort,sort2]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:managedObjectContext sectionNameKeyPath:"month.month_"
                                               cacheName:@"Root"];
0
votes

Fetch it all at once and use a Dictionary of arrays to sort it after it's returned. So, you loop through the results and created an array for each section and store it the array in a dictionary indexed for each section. Your Dictionary would look like

{ 
 {'January", "1,2,6,19,23,nil"},
 {'February", "2,7,18,19,28,nil"},
 {'March", "5,6,7,9,29,31,nil"},
 etc.
}

There is a good tutorial with code at icodeblog that gives an example of the details needed to do this.