Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:40:06+00:00 2026-06-07T17:40:06+00:00

I currently have a class which has a date object in it. This date

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:40:08+00:00Added an answer on June 7, 2026 at 5:40 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on an oophp application. I have a site class which will
I currently have a page which is declared as follows: public partial class MyPage
Currently I have a class that looks like this: public class MyClass : IMyClass
We currently have a utilities class that handles a lot of string formatting, date
I have a class with the fields deletionDate and experiationDate which could both be
I have a domain class which has two dates in it and I want
I have a table which has a list of invoices and their details: class
I have a program that gets the incoming number, date and time. I want
I've a document class which has an object which is a connection to a
I currently have a class file with the following enumeration: using System; namespace Helper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.