Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Sort By Date With FetchedResultsController into sections

I currently have a class which has a date object in it. This date object has both time and day in it. All this information gets loaded into a UITableViewCell via a NSFetchedResultsController. I need to sort the dates into sections where each section is the date without the Time. I also need each section to be sorted in itself by time. Here is what my current _fetchedResultsController looks like:

[self.managedObjectContext lock];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

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

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
                                               cacheName:@"Root"];
theFetchedResultsController.delegate = sender;
[self.managedObjectContext unlock];
return theFetchedResultsController;

The attribute I need to sort is the Entity.due attribute.

like image 703
CoreCode Avatar asked Jul 11 '12 01:07

CoreCode


1 Answers

You may want to check out the Core Data Sample called "DateSectionTitles" for more insight, I'm highlighting the basics from it.

To have sections according to date, you need to first sort by a normalized (or simplified) date. Try the following:

Add a transient attribute to your entity in the core data model editor. Ensure that it's type is NSDate and it's transient property is checked. Update your NSManagedObject class files by adding the property as a strong NSDate. Call it, "simplifiedDate". Also add another property to your .h file, call it "primitiveSimplifiedDate". Make it a strong.

In the .m use @dynamic for both the simplifiedDate and primitiveSimplifiedDate. This transient attribute should use return an NSDate that has been normalized to midnight. This level sets everything and allows you to establish the sections. I've used code along the lines of this:

-(NSDate *)simplifiedDate{
     // Create and cache the section identifier on demand.

    [self willAccessValueForKey:@"simplifiedDate"];
    NSDate *tmp = [self primitiveSimplifiedDate];
    [self didAccessValueForKey:@"simplifiedDate"];

    if (!tmp) {
        tmp=[self simplifiedDateForDate: [self due]];
        [self setPrimitiveSimplifiedDate: tmp];
    }

    return tmp;
}


-(NSDate *)simplifiedDateForDate:(NSDate *)date {

    if (!date) {
        return nil;
    }

    static NSCalendar *gregorian = nil;

    gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *newDateComponents=[gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit) fromDate:date];

    return [gregorian dateFromComponents:newDateComponents];
 }

If you so desired, this could also be an actual attribute that is calculated and stored when the actual date is set.

Next, you will have two sort descriptors. The first sort descriptor will be the above transient attribute, "simplifiedDate". The second attribute will be the "due" attribute. The "simplifiedDate", so long as the NSFetchedResultsController is setup correctly in the next step, will sort everything according to the section.

Finally you need to provide the section key name when you alloc/init the NSFetchedResultsController. In this case it will be "simplifiedDate". Like this:

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

Hope this helps.

t

like image 125
timthetoolman Avatar answered Nov 09 '22 17:11

timthetoolman