0

I'm building my first app in xcode and trying to fetch a list of people ordered by upcoming birthdays using coreData and NSFetchedResultController. My entity is setup as follows

uBirthdays
--------------
NSString uName
NSDate uBday

Here is my current code:

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"uBirthdays" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionNameGen" cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

[sort release];
[fetchRequest release];
[theFetchedResultsController release];

return _fetchedResultsController;    

}

Is it possibly to sort that by upcoming birthdays? or would I have to de-normalize my birthdays so I can sort by month and day only? Any input is greatly appreciated.

Thank you

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
lostincode
  • 39
  • 1
  • 1
  • 4

1 Answers1

1

Here's what you can do.

1) Set NSSortDescriptors for your uBDay and uName so your records will be sorted first by birthday, then by name.

2) Create a NSPredicate that pulls all birthdays between [NSDate now] (i.e. today) up to a certain time in the future (i.e. 30 days into the future = [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30])

Example code below:

#define kOneDayTimeInterval 86400 // this is in seconds: 86400 = 1 day

    - (NSFetchedResultsController *)fetchedResultsController {
        ...
        NSSortDescriptor *sortByBDay = [[NSSortDescriptor alloc] initWithKey:@"uBday" ascending:YES];
        NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"uName" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByBDay,sortByName, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [sortDescriptors release];
        [sortByName release];
        [sortByReleaseDate release];            
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(uBDay > %@) AND (uBDay <= %@)", [NSDate date], [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*30]];
        [fetchRequest setPredicate:predicate];
        ...
Rog
  • 18,602
  • 6
  • 76
  • 97
  • Thanks for your response. Since birthdays are stored with years from the past, wont this not work? Like if your birthday is 1983 it wont show up ever? – lostincode Dec 06 '10 at 21:52
  • You are absolutely right! In this case there is some NSDate formatting you will need to do - are you using a tableview to display these upcoming birthdays and grouping them in sections? – Rog Dec 06 '10 at 22:03
  • Yep, it's going into a tableview and I was able to figure out sections but sorting by 'upcoming birthdays' is where i'm lost due to the fact that years are in the past. – lostincode Dec 06 '10 at 22:10
  • Looks trickier than I first thought - I had a look around too and found this which indicates you will not be able to get this done at a persistent store level http://stackoverflow.com/questions/3455512/custom-sorting-using-categories-and-core-data-is-it-supported-by-the-framework – Rog Dec 06 '10 at 22:42