按核心数据按工作日分组 [英] Group by weekdays with Core Data

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

问题描述

在我的核心数据模型中,我有一个实体,它有一个日期属性,正如标题所示,我想将这个实体分组(周)天。

In my Core Data Model i've got an entity that has a date attribute and as the title suggests, i'd like to group this entity by (week)days.

问题是,日期或多或少地存储为时间戳,我不知道如何创建一个谓词,能够对我的实体进行适当的分组/过滤。

The problem is, the dates are stored more or less as timestamps and i don't know how to create a predicate that is capable of grouping/filtering my entities approprioately.

我已经想出,我可能需要做一个抓取每一天,所以创建以下方法。
代码是我需要帮助是正确的中间。

I've figured out that i will probably have to do a fetch for each day, so created following method. The code were i need help with is right in the middle of it.

- (NSFetchedResultsController *)fetchedResultsController:(NSDate *)day  {
if(fetchedResultsController != nil)
    return fetchedResultsController;

// Create and Configure Request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

// Predicate
// pseudo code where i'm clueless is marked by "<" and ">" - start
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DateAttribute BETWEEN <first second of day> AND <last second of day>"];
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<dayofmonth-month-year of DateAttribute> LIKE <dayofmonth-month-year of day>"];
[request setPredicate:predicate];
// pseudo code where i'm clueless is marked by "<" and ">" - end

// Sort descriptors
NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorName ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:titleDescriptor];
[request setSortDescriptors:sortDescriptors]; 

// create and init fetchResultsController
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;

//Memory
[request release];
[titleDescriptor release];
[aFetchedResultsController release];

return fetchedResultsController;

}

任何帮助。感谢

推荐答案

如果您想按工作日分组,最简单的做法是添加一个属性,如weekday您的实体;那么在初始化NSFetchedResultsController时,将weekday作为sectionNameKeyPath参数传递。在这种情况下,不需要指定谓词:您将自动获取工作日的部分。

If you want to group by weekday, the easiest thing to do is to add an attribute such as "weekday" to your entity; then you pass "weekday" as sectionNameKeyPath argument when initializing the NSFetchedResultsController. In this case there is no need to specify a predicate: you will automatically get sections for the weekdays.

如果要按天分组,请将DateAttribute作为sectionNameKeyPath参数传递当初始化NSFetchedResultsController时。再次,没有必要指定谓词:您将自动获取有关日期的部分。

If you want to group by day, you pass DateAttribute as sectionNameKeyPath argument when initializing the NSFetchedResultsController. Again, there is no need to specify a predicate: you will automatically get sections for the days.

您在代码中尝试执行的操作与您要求的操作不同。事实上,你不是试图回到部分(即按属性分组):你试图找回一个单独的部分,你的tableView包含你的实体描述的对象满足一个特定的谓词,即其DateAttribute下降(可能)在当天。为此,您可以使用以下代码:

What you are trying to do in your code differs from what you asked for. Indeed, you are not trying to get back sections (i.e. grouping by an attribute): you are instead trying to get back a single section for your tableView containing objects described by your entity satisfying a specific predicate, namely the ones whose DateAttribute falls (probably) in the current day. To achieve this, you can use the following code:

// start by retrieving day, weekday, month and year components for today
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];

// now build a NSDate object for the input date using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay]; 
[components setMonth:theMonth]; 
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
[components release];


// now build a NSDate object for tomorrow
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
[offsetComponents release];

NSDateComponents *tomorrowComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:nextDate];
NSInteger tomorrowDay = [tomorrowComponents day];
NSInteger tomorrowMonth = [tomorrowComponents month];
NSInteger tomorrowYear = [tomorrowComponents year];

[gregorian release];


// now build the predicate needed to fetch the information
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"DateAttribute < %@ && DateAttribute > %@", nextDate, thisDate];

此谓词将精确检索其DateAttribute属于当前日期的所有对象。希望这有助于。

This predicate will retrieve exactly all of the objects whose DateAttribute falls within the current day. Hope this helps.

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

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