在我的核心数据模型中,我有一个具有日期属性的实体,顾名思义,我想按(周)天将该实体分组。

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

我已经发现我可能每天都必须进行提取,因此创建了以下方法。
我需要帮助的代码就在其中。

- (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;


}

我真的很感谢您的帮助。谢谢

最佳答案

如果要按工作日分组,最简单的方法是在实体中添加“工作日”之类的属性。然后在初始化NSFetchedResultsController时将“工作日”作为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];


该谓词将精确检索其DateAttribute在当日之内的所有对象。希望这可以帮助。

关于iphone - 按工作日按核心数据分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1260014/

10-13 07:31
查看更多