核心数据按日期排序使用FetchedResultsController分段 [英] Core Data Sort By Date With FetchedResultsController into sections

查看:139
本文介绍了核心数据按日期排序使用FetchedResultsController分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个类中有一个日期对象。此日期对象中包含时间和日期。所有这些信息通过 NSFetchedResultsController 加载到 UITableViewCell 中。我需要将日期排序,其中每个部分是日期而不是时间。我还需要每个部分按时间排序。这是我目前的_fetchedResultsController看起来像:

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;

我需要排序的属性是 Entity.due 属性。

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

推荐答案

您可能想要查看核心数据示例名为DateSectionTitles

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:

在核心数据模型编辑器中向您的实体添加一个临时属性。确保它的类型是NSDate,并且它的transient属性被选中。通过将属性添加为强NSDate来更新NSManagedObject类文件。调用它,simplifiedDate。还要为.h文件添加另一个属性,称为primitiveSimplifiedDate。

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.

在.m中为simplifiedDate和primitiveSimplifiedDate使用@dynamic。这个瞬时属性应该使用返回一个NSDate已经规范化为午夜。此级别设置所有内容,并允许您建立节。我使用的代码如下:

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.

接下来,您将有两个排序描述符。第一个排序描述符将是上面的临时属性simplifiedDate。第二个属性将是due属性。 simplifiedDate,只要NSFetchedResultsController在下一步中正确设置,就会根据该部分对所有内容进行排序。

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.

最后,您需要提供节键名称当你alloc / init NSFetchedResultsController时。在这种情况下,它将是simplifiedDate。像这样:

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"];

希望这有助。

t

这篇关于核心数据按日期排序使用FetchedResultsController分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆