按工作日分组核心数据

gab*_*tub 2 iphone cocoa-touch core-data objective-c iphone-sdk-3.0

在我的核心数据模型中,我有一个具有日期属性的实体,正如标题所暗示的那样,我想在(星期)之前对该实体进行分组.

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

我已经发现我可能每天都要进行一次抓取,所以创建了以下方法.我需要帮助的代码正好在它的中间.

- (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;
Run Code Online (Sandbox Code Playgroud)

}

我真的很感激任何帮助.谢谢

Mas*_*aro 6

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

如果要按天分组,则在初始化NSFetchedResultsController时将DateAttribute作为sectionNameKeyPath参数传递.同样,不需要指定谓词:您将自动获取日期的部分.

您在代码中尝试做的与您要求的不同.实际上,您并不是想要返回部分(即通过属性进行分组):而是尝试返回tableView的单个部分,其中包含满足特定谓词的实体描述的对象,即DateAttribute下降的对象(可能)在当天.要实现此目的,您可以使用以下代码:

// 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];
Run Code Online (Sandbox Code Playgroud)

该谓词将准确检索DateAttribute在当前日期内的所有对象.希望这可以帮助.