【问题标题】:MagicalRecord - fetch objects grouped by month/yearMagicalRecord - 获取按月/年分组的对象
【发布时间】:2013-10-21 07:54:30
【问题描述】:

我有许多 Game 对象,每个对象都有一个 date 参数。

在我的控制器中,我想获取这些游戏,按日期的month-year 组合分组 - 这样我可以获得月份列表,并且每个月 - 发生在那个月。

是否有一个简单的解决方案,无需修改我的 CoreData 模型?

【问题讨论】:

    标签: iphone database core-data magicalrecord


    【解决方案1】:

    Custom Section Titles with NSFetchedResultsController - 这个示例项目展示了如何做到这一点(如果我理解正确的话)。很明显,它没有使用 MagicalRecord,但应该很容易移植到 MR。

    【讨论】:

      【解决方案2】:

      这对我有用

      在你的

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 2017-03-24
        • 2021-10-16
        相关资源
        最近更新 更多