Sometimes you want to order your NSManagedObjects by date, in this case I would like to group objects by relative date like Today, Yesterday, etc…

I’m using DateTools in order to make easy some relative dates, for the long time distance dates I’m using the default doesRelativeDateFormatting from NSDateFormatter.

This category allows you to create sections in your UITableView sorted by relative date by just setting the sectionNameKeyPath in your NSFetchedResultsController to the property available on the previous category:

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
 managedObjectContext:self.coreDataManager.managedObjectContext
 sectionNameKeyPath:@"relativeDate"
 cacheName:nil];

PS: I have - (NSDate *)dateOfToday as a NSDate category, but in this case I put everything inside the same file to make this example clearer.



Comments