In my Core Data Model i've got an entity that has a date attribute and as the title suggests, i'd like to group this entity by (week)days.
The problem is, the dates are stored more or less as timestamps and i don't know how to create a predicate that is capable of grouping/filtering my entities approprioately.
I've figured out that i will probably have to do a fetch for each day, so created following method. The code were i need help with is right in the middle of it.
- (NSFetchedResultsController *)fetchedResultsController:(NSDate *)day {
if(fetchedResultsController != nil)
return fetchedResultsController;
// Create and Configure Request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Predicate
// pseudo code where i'm clueless is marked by "<" and ">" - start
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DateAttribute BETWEEN <first second of day> AND <last second of day>"];
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<dayofmonth-month-year of DateAttribute> LIKE <dayofmonth-month-year of day>"];
[request setPredicate:predicate];
// pseudo code where i'm clueless is marked by "<" and ">" - end
// Sort descriptors
NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorName ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:titleDescriptor];
[request setSortDescriptors:sortDescriptors];
// create and init fetchResultsController
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
//Memory
[request release];
[titleDescriptor release];
[aFetchedResultsController release];
return fetchedResultsController;
}
I'd really appreciate any help. Thanks