我有多个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];