按工作日分组核心数据

时间:2009-08-11 12:23:20

标签: iphone objective-c cocoa-touch iphone-sdk-3.0 core-data

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

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

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

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

}

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

2 个答案:

答案 0 :(得分: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];

此谓词将准确检索DateAttribute在当前日期内的所有对象。希望这会有所帮助。

答案 1 :(得分:4)

你好@gabtub 我实际上在看你的问题,我有一个同样的问题。 我想要做的是将我的表分成几个部分,而不是你提到的时间戳。所以我所做的是在我的类中添加了一个新方法,它扩展了NSManagedObject(我正在获取的实体),该方法返回一个格式化的日期:

- (NSString *)transactionDay{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
return [dateFormatter stringFromDate:self.date];}

然后将我的fetchedResultController更改为:

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"transactionDay" cacheName:nil];

一切都充满魅力。

查看链接以查看更多详细信息。 http://www.IPHONE4GAPP.NET/superdb-core-data-app-with-sections.html