2013-10-21 31 views
0

我有多個Game對象,每個對象都有一個date參數。MagicalRecord - 按月份/年分組的獲取對象

在我的控制器中我想獲取這些遊戲,由month分組 - 他們的日期year組合 - 這樣我就可以得到個月的列表,併爲每個月份 - 在這一個月發生的遊戲列表。

有沒有一個簡單的解決方案,而不必修改我的CoreData模型?

回答

0

這在

model.h 

工作對我來說

添加

-(NSString *)yearMonth; 

model.m

-(NSString *)yearMonth 
{ 
    NSDateFormatter *dateFormatter = [NSDateFormatter new]; 
    [dateFormatter setDateFormat:@"yyy-MM"]; 

    NSString *formattedDateString = [dateFormatter stringFromDate:self.eventDate]; 
    return formattedDateString; 
} 

然後用這樣的組方法調用MR_Fetch:

NSFetchedResultsController *frc = [model MR_fetchAllGroupedBy:@"yearMonth" withPredicate:nil sortedBy:@"eventDate" ascending:NO];