我有多個Game
對象,每個對象都有一個date
參數。MagicalRecord - 按月份/年分組的獲取對象
在我的控制器中我想獲取這些遊戲,由month
分組 - 他們的日期year
組合 - 這樣我就可以得到個月的列表,併爲每個月份 - 在這一個月發生的遊戲列表。
有沒有一個簡單的解決方案,而不必修改我的CoreData模型?
我有多個Game
對象,每個對象都有一個date
參數。MagicalRecord - 按月份/年分組的獲取對象
在我的控制器中我想獲取這些遊戲,由month
分組 - 他們的日期year
組合 - 這樣我就可以得到個月的列表,併爲每個月份 - 在這一個月發生的遊戲列表。
有沒有一個簡單的解決方案,而不必修改我的CoreData模型?
Custom Section Titles with NSFetchedResultsController - 這個示例項目展示瞭如何做到這一點(如果我正確理解你的話)。顯然,它不使用MagicalRecord,但它應該很容易移植到MR。
這在
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];