0

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

Community
  • 1
  • 1
nmdias
  • 3,888
  • 5
  • 36
  • 59
  • 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 Aug 31 '12 at 20:33
  • 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 Aug 31 '12 at 22:26

2 Answers2

1
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"];
Daniel Broad
  • 2,512
  • 18
  • 14
  • First of all, Thank you very much for your help @dorada! I bet your code should work. I implemented it, but it's no good. I don't know why I don't get my sections. Do you happen to know if sectionNameKeyPath:"month.month_" can refer to a NSNumber in my model? Is this allowed? Or month_ property must be a NSString? – nmdias Aug 31 '12 at 23:46
  • 1
    My problem wasn't here. I was not implementing - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section. After doing so, I got my sections displayed. I'm placing your answer as correct, because it is actually correct and it was indeed helpful nonetheless. Thank you =) – nmdias Sep 01 '12 at 00:25
0

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.

Mark S.
  • 3,849
  • 4
  • 20
  • 22
  • Appreciate the suggestion, but I'm really looking to use NSFetchedResultsController. Thank you @Mark – nmdias Aug 31 '12 at 23:50