获得NSFetchedResultsController,NSSortDescription和sectionNameForKeyPath一起工作 [英] Getting NSFetchedResultsController, NSSortDescription and sectionNameForKeyPath to work together
问题描述
我目前正在处理一个应用程式,该应用程式有几个实体和关系,如下所示:
I'm currently working on an App that has a couple of Entities and relationships as illustrated below:
项目< - > 类别。
Item <<--> Category.
我目前正在抓取项目实例,并使用项目的 category.name
。在这种情况下,我可以使用排序描述符按名称对类别进行排序,这是非常简单和工作正常(相关代码如下):
I am currently fetching Item instances and displaying them in sections using the item's category.name
. In this case I can use a sort descriptor to sort the categories by name, which is pretty straightforward and working fine (relevant code below):
-(NSFetchedResultsController*)fetchedResultsController {
if (fetchedResultsController_ != nil)
return fetchedResultsController_;
NSManagedObjectContext *moc = [order_ managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"category.name"
cacheName:nil];
controller.delegate = self;
self.fetchedResultsController = controller;
[controller release];
[fetchRequest release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
// Error handling
}
return fetchedResultsController_;
}
我现在的问题是,我需要排序这些类别不是名称,由属于类别实体一部分的(NSNumber *)displayOrder
属性。 但我需要表格视图中的章节标题,继续使用类别的名称
。
My problem now is that I need to sort these categories not by name, but by a (NSNumber*) displayOrder
attribute that is part of the Category entity. BUT I need the section titles on the tableview to continue to use the categorie's name
.
如果我设置sortDescriptor使用 category.displayOrder
并保持sectionNameKeyPath为 category.name
工作正常,但sortDescriptor被fetchedResultsController忽略,表部分按类别的名称排序(不知道为什么?)。
If I set the sortDescriptor to use category.displayOrder
and keep the sectionNameKeyPath as category.name
, the section titles work fine but the sortDescriptor is simply ignored by the fetchedResultsController and the table sections are ordered by the category's name (not sure why??).
我的下一个想法是覆盖 displayOrder
getter方法,但是没有让我太远,因为返回类型不同,加上我需要实际的displayOrder值进行段排序。
My next idea was to overwrite the displayOrder
getter method but that didn't get me too far as the return types are different, plus I needed the actual displayOrder value for the section sorting.
现在我有一个解决方案感觉有点笨拙(下面的代码),我想知道是否有一个更好的方法实现相同的东西,使用fetchedResultsController单独。
So right now I have a solution which feels a bit clunky (code below), and I am wondering if there is a better way of achieving the same thing using the fetchedResultsController alone.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
// The code below grabs a reference to first object for a given section
// and uses it to return the associated category name
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSArray *menuItems = [sectionInfo objects];
if ([menuItems count] > 0)
{
MenuItem *menuItem = [menuItems objectAtIndex:0];
NSString *categoryName = menuItem.category.name;
return categoryName;
}
return [sectionInfo name];
}
我在这里缺少基本的东西吗?
Am I missing something basic here?
先感谢您的想法。
Rog
推荐答案
这是一个很好的解决方案,Rog。
That's a perfectly good solution to the problem, Rog.
你肯定不想/需要子类NSFetchedResultsController。
You certainly don't want/need to subclass NSFetchedResultsController.
@aroth,我们没有足够的信息来了解他的对象模型的细节,但是这些名称肯定意味着不拥有项。项目有一个类别。他的目的是显示一个项目列表,这就是为什么他正在抓取项目。
@aroth, we don't have enough information to know the details of his object model, but the names certainly imply that category does not own item. Item has a category. His intent is to display a list of items, that is why he is fetching items.
这篇关于获得NSFetchedResultsController,NSSortDescription和sectionNameForKeyPath一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!