6

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.

CoreCode
  • 2,227
  • 4
  • 22
  • 24
  • Are you able to fetch the NSDate objects and display them in your UITableView? In other words, is the problem just the display and sorting? – Jamie Jul 11 '12 at 03:42
  • @Jamie It's just a problem sorting them into sections by date (that ignores hours, minutes and seconds) – CoreCode Jul 11 '12 at 05:34

1 Answers1

9

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

timthetoolman
  • 4,613
  • 1
  • 22
  • 22
  • So would I add a new property to the subclass and add this method? How would I make to property and the new method work together? What should the properties of the property look like (strong, weak, nonatomic, NSDate, dynamic etc.). Thanks – CoreCode Jul 11 '12 at 05:54
  • Thanks, when I get a chance, I will try it out to see if it works – CoreCode Jul 11 '12 at 07:53
  • There is "No visible interface that declares the selector 'primitiveSimplifiedDate' is the error I am getting in the [self primitiveSimplifiedDate] method call – CoreCode Jul 11 '12 at 08:07
  • please add the primitiveSimplifiedDate as a property, see updated code above. – timthetoolman Jul 11 '12 at 14:09
  • I am getting the error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath simplifiedDate not found in entity ' – CoreCode Jul 14 '12 at 04:50
  • Did u add simplifiedDate to your core data model? If you an existing store using the old model u need to make sure you set the necessary mugration options in the setup of your persistent store controller. – timthetoolman Jul 14 '12 at 06:52
  • Yeah I did add it to my data model and I actually reformatted my mac (hence the delay in my reply) a day ago so the simulator was fresh – CoreCode Jul 14 '12 at 09:04
  • I think the issue may be that the transient property is part of the fetch as a sort descriptor. Remove the simplifiedDate sort descriptor and give it a shot. – timthetoolman Jul 14 '12 at 17:27