我在核心数据中有两个实体,它们之间有一对多的关系(Dream与符号有一对多的关系)。我还通过使用谓词和搜索发生时调用的相应searchFetchedResultsController来提供搜索功能。 现在我想使用搜索栏范围按钮实现搜索范围功能。如何更改我的谓词,以便它根据符号过滤内容,而不是过滤正常工作的梦想。我是否需要创建另一个fetchedResultsController或者我应该修改此searchFetchedResultsController中的谓词以获得所需的结果???
- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dream"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:NO] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Configure predicate
NSPredicate *filterPredicate = nil;
if (searchString.length) {
filterPredicate = [NSPredicate predicateWithFormat:@"note CONTAINS[cd] %@", searchString];
}
[fetchRequest setPredicate:filterPredicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
aFetchedResultsController.delegate = self;
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return aFetchedResultsController;
}
答案 0 :(得分:2)
无需单独的获取结果控制器。
谓词可能是这样的:
fetchRequest.predicate = [NSPredicate predicateWithFormat:
@"ANY symbols.name CONTAINS[cd] %@", searchString];