【发布时间】:2009-10-17 22:47:31
【问题描述】:
我正在使用一个 NSFetchedResultsController,它的 fetchRequest 有一个谓词。但是,每次执行时,查询似乎都没有给我一致的分组。
我为 NSFetchedResultsController 设置了“sectionNameKeyPath”,并且根据在运行提取之前是否一直在使用根对象,我得到了不同数量的返回部分。有时我得到 3 个部分,而其他时候,它返回 1 个部分,即预期结果。
我如何创建 FetchRequestController:
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Configure the request's entity and its predicate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// The predicate to find all employees associated with a Group
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.groups IN %@",
[division groups]];
[fetchRequest setPredicate:predicate];
// Sort based on create date and time
NSSortDescriptor *createDateSortDcptor = [[NSSortDescriptor alloc] initWithKey:@"createDateTime" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:createDateSortDcptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// should be grouped by the 'Group' employee belongs to.
NSFetchedResultsController *controller =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:@"groups"
cacheName:@"Root"];
我的对象模型与其他问题中概述的相同:
https://stackoverflow.com/questions/1580236/how-to-setup-a-predicate-for-this-query 有没有办法确保我每次都得到一致的分组?
【问题讨论】: