我NSFetchedResultsController
用来驱动我的数据UITableViewController
。我正在获取的托管对象的简化版本如下所示(例如):
Person:
-(BOOL)hasPet;
-(BOOL)hasDog;
基本上,我希望我的表格只显示那些Person
有宠物的对象。所以这很简单,我可以使用谓词。现在,对于那些hasPet == YES
,我想将它们分成 2 个部分放在表中,第一个部分hasDog == YES
是 ,第二个是hasDog == NO
。这是我有点模糊的地方。这是我配置结果控制器的地方,希望有人可以帮助我朝着正确的方向前进。
- (NSFetchedResultsController *)fetchedResultsController {
if (nil != fetchedResultsController) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hasPet == %@", [NSNumber numberWithBool:YES]];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *dogDescriptor = [[NSSortDescriptor alloc] initWithKey:@"hasDog" ascending:YES];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dogDescriptor, sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aController.delegate = self;
self.fetchedResultsController = aController;
// Release things
return fetchedResultsController;
}
排序描述符是我需要集中注意力的地方吗?