2009-08-11 38 views
2

在我的核心數據模型中,我有一個具有日期屬性的實體,並且如標題所示,我希望將該實體按(星期)天分組。按核心數據平日分組

問題是,日期作爲時間戳存儲或多或少,我不知道如何創建一個能夠適當地分組/過濾我的實體的謂詞。

我已經想通了,我可能不得不爲每一天做一個提取,所以創建了以下方法。 代碼是我需要幫助的是正確的在它的中間。

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

}

我真的很感激任何幫助。謝謝

回答

5

如果您想按工作日進行分組,最簡單的方法是將「weekday」等屬性添加到實體中;那麼在初始化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當天之內的物體。希望這可以幫助。

+0

感謝您的全面回覆! 「 」如果你想按天分組,你可以在初始化NSFetchedResultsController時傳遞DateAttribute作爲sectionNameKeyPath參數。同樣,不需要指定謂詞:你將自動獲取這些日子的分段。 如果能夠工作,它會很好,但是當這樣做時,它將按照DateAttribute的值(包括時間,例如「2009-07-02 20:51:27 -0400」) p.s.:我的最終目標是獲得一個工作日的列表,當點擊其中一個時,與當天連接的所有實體都將在另一個tableview中打開。 – gabtub 2009-08-12 07:59:27

+0

您可以通過在實體中存儲DateAttribute而無需使用我在示例代碼中顯示的相同技術來實現此目的:從NSDate對象(您的DateAttribute)開始,首先提取其日期組件,然後使用另一個NSDate對象構建另一個NSDate對象這些組件(因此它不包含時間)。這是您存儲在實體中的最後一個NSDate對象。然後,根據所解釋的日常工作檢索對象。 – 2009-08-12 09:56:49

+0

起初我並不想修改數據模型,因爲它是由其他人指定的,但幸運的是,事實證明,我們確實不需要存儲時間和日期,但只有日期,因此我使用了部分示例代碼= >非常感謝 非常感謝! – gabtub 2009-08-12 15:58:22

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

+0

有趣,謝謝! – gabtub 2010-09-28 11:32:19

+0

不要忘記釋放dateFormatter。 – 2011-03-13 14:46:07