我有一个托管对象来表示地图上的一个点。这些点有零多类型。我还想显示一个分段的表格视图。当类型是 MapPOI 对象上的单个值时,我使用 NSFetchedResultsController 进行此操作。但是现在类型是在不同的对象中,与“MapPOI”之间的关系称为“类型”,我该如何编写查询(可以吗?)
原来的:
- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MapPOI"];
if(searchString.length)
{
request.predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchString];
}
request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"type" ascending:YES ],[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES ],nil ];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.campus.managedObjectContext
sectionNameKeyPath:@"type"
cacheName:nil];
aFetchedResultsController.delegate = self;
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error])
{
NSLog(@"Error performing institution fetch with search string %@: %@, %@", searchString, error, [error userInfo]);
}
return aFetchedResultsController;
}
我试过类似的东西
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.campus.managedObjectContext
sectionNameKeyPath:@"types.name"
cacheName:nil];
但这导致
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid to many relationship in setPropertiesToFetch: (types.name)'