ios MagicalRecord sorting. Need a list of section headers

你离开我真会死。 提交于 2020-01-06 14:15:32

问题


I have a Core Data model accessed using MagicalRecord. I use this code to get a tableview sorted and grouped by categories:

frc = [ListActivity MR_fetchAllSortedBy:@"activityCategory,activityName"
                                  ascending:YES withPredicate:nil
                                    groupBy:@"activityCategory"
                                   delegate:nil];

The resulting tableview looks like this:

In another view, I use the same data, but this time want only a list of the section headers, represented by the attribute "activityCategory". I expected it to be a simple matter, but not so. This code, for example:

frc = [ListActivity MR_fetchAllSortedBy:@"activityCategory"
                              ascending:YES withPredicate:nil
                                groupBy:nil
                               delegate:nil];

yields this:

Instead, what I want is just a list of the categories (singularly), the attributes that provide the section headers in the first example. I tried a couple of other configurations, but realized I was just throwing stuff against the wall to see if it would stick. The documentation for MagicalRecord, as excellent as the framework is, is lamentably scant.

Can someone please point me at the solution?

Thanks!


回答1:


My best guess is you'll need to configure your fetch request a bit more before firing the fetched results controller. MagicalRecord uses a naming convention such that if you change find to request, it'll instead return the NSFetchRequest object. So, for your case here, you'll need something like

NSFetchRequest *request = [ListActivity MR_requestAllSortedBy:@"activityCategory" ascending:YES inContext:context];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                          managedObjectContext:context
                                            sectionNameKeyPath:nil
                                                     cacheName:nil];
[frc MR_performFetch];



回答2:


You might want to consider having a ListActivityCategory entity as well as storing the category as a String on ListActivity

That way you could fetch those instead of haven to 'unique' across all the returned ListActivity objects



来源:https://stackoverflow.com/questions/21900441/ios-magicalrecord-sorting-need-a-list-of-section-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!