问题
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