1

我在核心数据中有两个实体,它们之间存在一对多关系(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;
}  
4

1 回答 1

2

不需要单独的获取结果控制器。

谓词可能是这样的:

fetchRequest.predicate = [NSPredicate predicateWithFormat:
   @"ANY symbols.name CONTAINS[cd] %@", searchString];
于 2012-10-24T07:04:42.917 回答